Npm: Run postinstall script only when installed as dependency - npm

I developed a npm package ("node_commons") which is included in my other project like this:
package.json (other project)
"node-commons": "git+ssh://git#stash.custom.domain.net:7999/npm/libs/node_commons.git"
The node_commons package is written in ES6 but this version is not supported later, therefore I use a postinstall script to transpile it with babel.
package.json (node_commons)
"postinstall": "babel src -d src"
This works fine. When the package is included as dependency in my project, the files are transpiled.
My problem: When I develop the node_commons package I use npm install to install the internal dependencies. But then I do not want to transpile it. I only want to transpile, when the package is installed as dependency (e.g. in my other project). Is there a way to do this?
Something like this:
package.json (node_commons)
"postinstall-as-dependency": "babel src -d src"

Create .no-postinstall file in module root.
Mac & Linux: Add following to package.json: "postinstall": "if [ ! -e .no-postinstall ]; then babel src -d src; fi"
Windows: Add following to package.json: "postinstall": if exist .no-postinstall () else (babel src -d src)"
Cross Platform (more work):
You can create a node script (i.e. ./my-script.js) and using fs module, check existence of .no-postinstall and execute babel src -d src using child_process.exec() as described in official doc here. Add your script to package.json: "postinstall": node my-script.js
Please note that I use Mac, so I can't verify Windows version.
Explanation
if [ ! -e .no-postinstall ] checks non-existence (with negation operator !) of given file. If file does not exist, it executes your script. Since you add .no-postinstall file to your module root, script does not get executed when you install your internal modules. On the other hand, modules installing your module as a dependency do not have .no-postinstall file in their root and your script get executed.
.no-postinstall is not a special name, you can use whatever name you choose.

In my case, I had a postinstall script that I only wanted to run when the package was NOT installed as a dependency.
Here is the package.json script I used:
{
[...]
"scripts": {
"#comment_postinstall": "Behavior of the node-js subscript below: if '.git' doesn't exist under my-lib, it's installed as a dep, so do nothing -- else, return error-code 1, causing patch-package to run.",
"postinstall": "node -e \"let myLibAsDep = !require('fs').existsSync('.git'); if (!myLibAsDep) process.exit(1);\" || patch-package"
},
[...]
}
You can of course just reverse the if (!myLibAsDep) if you want your script to run only when installed as a dependency.

If I understand correctly, you want your package to run a postinstall script only if user install it as a dependency (npm install node-common)?
When your postinstall script runs, it has the npm_config_save_dev available to it, which is 'true' when users install the package with the --save-dev flag:
"postinstall": "! [ $npm_config_save_dev ] && echo \"Installed as a dependency\" || \"Installed as a dev dependency\""

I had the same issue, I had a postinstall script which I wanted to run only when the package is installed as a dependency.
At the top of my postinstall script I added:
if (!process.cwd().includes('node_modules')) {
return;
}
and then it only ran when the module existed under a node_modules dir.

This should do the trick:
if (process.env.INIT_CWD === process.cwd())
process.exit()
Works with npm and pnpm. Don't know about yarn
https://docs.npmjs.com/cli/v8/using-npm/scripts#best-practices

I created a package to solve this problem, inspired by the answer from #venryx.
https://github.com/douglasjunior/ignore-dependency-scripts
Usage
Replace this:
// package.json
"name": "my-library",
"scripts:" {
// "start", "test", "build", etc
"postinstall/preinstall/prepare/etc": "your && scripts && here"
},
With this:
// package.json
"name": "my-library",
"scripts:" {
// "start", "test", "build", etc
"postinstall/preinstall/prepare/etc": "npx --yes ignore-dependency-scripts \"your && scripts && here\""
},
Replace your && scripts && here by any post/pre install script that you want, like husky install, npx pod-install or both.
Now, when you run yarn install or npm install in ./my-library the your && scripts && here will run normally.
But, when you install my-library as a dependency (aka yarn add url/to/my-library.git) in another repository, the your && scripts && here will be ignored.

Related

husky / lint-staged not running custom command or printing errors

I am trying to upgrade from husky#4.8 and lint-staged#11.2 to husky#8.0 and lint-staged##13.1 for my Nx repo. It seems that the only configuration I used to need was the following in package.json:
"scripts: {
"lint:fix": "ng lint -- --fix && npm run prettier:fix",
}
"husky": {
"hooks": {
"commit-msg": "commitlint -e $HUSKY_GIT_PARAMS"
}
},
"lint-staged": {
"*.{ts,js,css,md,html}": "npm run lint:fix",
"relative": true
}
This no longer works with the new husky and lint-staged. I followed this tutorial and installed husky: npx husky-init && npm install, installed lint-staged: npm install --save-dev lint-staged, modified the .husky/pre-commit file:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
and added the .lintstagedrc.json file:
{
"*.ts": [
"prettier --write",
"eslint"
],
"*.html": [
"eslint",
"prettier --write"
],
"*.scss": "prettier --write"
}
When I make a new commit it seems to run. However it looks like this command either runs on every library or every staged file (I have a lot of libraries). When I change the file contents to:
{
"*.{js,ts,json,scss,md}": ["npm run lint:fix"]
}
it takes a while to run (though it seems to run and fix the linting) AND I don't get the output of my ng lint -- --fix command. If I go into the .husky/pre-commit file and change npx lint-staged to npm run lint:fix, it runs the ng lint -- --fix command, fixes what it can and prints out linting errors. However, the error doesn't prevent the commit and I have to commit any new changes that the linter fixed. With the old versions I didn't need to do this. Also I didn't need to commit a .husky/pre-commit file into my repo. (I see some workarounds that have a postinstall script that generates this file for the user).
How can I utilize lint-stage and husky newest versions to run my custom npm command so I can call ng lint like in the older versions and block commits on errors? Should I just use the older versions? Also what benefit do the newer versions provide?

run npm script after package installing

I have a simple git repository https://github.com/alexey-sh/test_webpack_with_npm
I want to use it as npm dependency but without publishing in npm.
So in other words I want to install the package in my another repository as dependency.
npm i --save git+https://github.com/alexey-sh/test_webpack_with_npm.git
But I want to use a modern js in my test_webpack_with_npm project and compile it into old javascript after package installation process. To achieve it I created npm scripts (test_webpack_with_npm package)
"scripts": {
"install": "npm run build",
"build": "webpack --mode production --config webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
so now there's a weird thing:
if I run npm run build from test_webpack_with_npm repository I can get dist/index.js without class declaration (as I expected).
but if I install the package via the following command
npm i --save git+https://github.com/alexey-sh/test_webpack_with_npm.git
I get another type of dist/index.js and there are class declaration.
How can I install the test_webpack_with_npm properly? I want to see old js in node_modules/test_webpack_with_npm/dist/index.js.
Steps to reproduce:
mkdir my_test_project
cd my_test_project
npm init
npm i --save git+https://github.com/alexey-sh/test_webpack_with_npm.git
check node_modules/test_webpack_with_npm/dist/index.js
Thanks!
the fix is very simple. just replace exclude with include in webpack config
include: path.join(__dirname, 'sources'),
that works perfectly.
updated config goes here https://github.com/alexey-sh/test_webpack_with_npm/blob/master/webpack.config.js

How do I run an npm script of a dependent package

I have a package that itself has a script in its package.json that I would like to be able to run in my top-level project. This package is one of my top-level projects dependencies. I'm looking for a way to directly or indirectly call the dependency packages script.
Let us assume the module name I'm working with is named foo and the script I want to run is updateFooData.
I tried using the syntax npm run-script <package> <...> to run it, but this appears to be deprecated functionality as I can't find it in the current official documentation but I see it in other (very old) search results.
npm run-script foo updateFooData
# npm ERR! missing script: foo
I also looked into the npm api and while npm.commands.run-script(args, callback) will do what I want, I can't figure out how to load the module into npm
{
...
"scripts":{
"foo:updateFooData": "node --eval \"... ??; npm.commands.run-script('updateFooData', callback)\""
}
}
npm run foo:updateFooData
# Obviously fails
The only thing I've found that works so far is to CD into the submodule directory and run npm from there. This is not the preferred solution for me.
cd node_modules/foo
npm run updateFooData
I ran into this trying to run the updatedb script for geoip-lite. You should use the npm explore command which will spawn a new shell in a dependencies' directory.
So for your use case, try npm explore foo -- npm run updateFooData
Note:
This isn't a very good idea. You have no guarantee which node_modules folder module will be installed in as NPM will attempt to optimise space by installing shared packages at the highest level possible. – #superluminary
Something I've found that does work:
If the script you are running runs a script file, you can look at the path of the file it's running and run the script using a require:
# if node_modules/foo/package.json looks like this
{
"scripts": {
"updateFooData":"scripts/updateFooData.js"
}
}
# then package.json can look like this
{
"scripts": {
"foo:updateFooData":"node --eval \"require('foo/scripts/updateFooData.js')\""
}
}
# or package.json can look like this
{
"scripts": {
"foo:updateFooData":"node node_modules/foo/scripts/updateFooData.js"
}
}
# and you can run it like this
npm run foo:updateFooData
I don't like this solution because it only works if the npm script you are running is a file. It won't apply in all

Install only one package from package.json?

Suppose that somewhere in my package.json I have:
"dependencies": {
"bower": "1.0.0",
// zillion other dependencies
}
Is there a way to make npm install only bower#1.0.0 from my package.json? Like so: npm install --only bower.
My goal is to make npm install and bower install run simultaneously.
As a workaround you may use something like:
$ node -pe "require('./package').dependencies.bower"
// → 1.0.0
$ npm install bower#$(node -pe "require('./package').dependencies.bower")
// npm install bower#1.0.0
// or with jq
$ npm install bower#$(< package.json jq -r '.dependencies.bower')
Where -e/--eval flag evaluates passed string and -p/--print prints result of eval.
💡 Please consider other answers as well since this one may be outdated.
As #atalantus noted in comment, the accepted answer doesn't work on newer version of NPM. Working solution for newer versions (verified on NPM 6.13.4) is:
npm install --no-package-lock --no-save bower#1.0.0
This will install bower and all its dependencies, but prevents installation of anything else you might have in package.json. It also doesn't create or modify existing package-lock.json.
From npm documentation:
The --no-package-lock argument will prevent npm from creating a package-lock.json file. When running with package-lock's disabled npm will not automatically prune your node modules when installing.
--no-save: Prevents saving to dependencies.
Combining this with Anton Rudeshko's approach to find out version in package.json, the final solution is:
VERSION_BOWER=`node -p -e "require('./package.json').dependencies.bower"`
npm install --no-package-lock --no-save bower#"$VERSION_BOWER"
Update 2023
Alternative solution I've just used is to temporarily modify package.json to only keep the dependency you need. That can be done quite easily with help of jq
Given package.json
{
"name": "sample",
"main": "src/index.js",
"dependencies": {
"bower": "1.0.0",
"foo": "^1.2.3",
"bar": "^4.5.6",
},
"devDependencies": {
"baz": "^10.20.30",
}
}
Let's first modify it with jq to only keep bower
cat package.json| jq 'del(.devDependencies) | .dependencies |= {bower:.bower}'
Gives us
{
"name": "sample",
"main": "src/index.js",
"dependencies": {
"bower": "1.0.0"
}
}
by deleting devDependencies completely and leaving only bower in dependencies.
Next up you would want to overwrite package.json with this new, version, but you can't do it directly like this
cat package.json | jq 'del(.devDependencies) | .dependencies |= {bower:.bower}' > package.json
because the file gets overwritten before it's fully read. Instead you need to buffer the output stream of jq command, simplest solution just storing it in variable, and then overwriting the original file
updated=`cat package.json | jq 'del(.devDependencies) | .dependencies |= {bower:.bower}'`
echo $updated > package.json
And package.json now reads:
cat package.json
{
"name": "sample",
"main": "src/index.js",
"dependencies": {
"bower": "1.0.0"
}
}
Run npm install now and you only install bower#1.0.0.
Obviously not always we can afford to modify the file, but you could also back-up the original file, etc. Main point is here how to easily modify json file thanks to jq rather than playing magic tricks with sed :-)
Seeing as with newer versions of NPM the previous solutions don't work anymore, I came up with this:
npm i -g --prefix=$(pwd) bower # $(pwd) = current directory, UNIX only
node bin/bower
Installing a package globally allows you to specify a "prefix" - a directory where the package will be installed. Here I just put the current directory as the folder I want the package to appear in. In CI, this will probably be your repo root.
Keep in mind this will ignore your package.json version and just download the latest one. If you need a specific version, just use bower#someversion instead.
An alternative solution is just rm package.json && npm i bower, or mv package.json _package.json && npm i bower if you want to restore it later

How to install grunt and how to build script with it

Hi I'm trying to install Grunt on Windows 7 64 bit. I have installed Grunt using commands
npm install -g grunt
npm install -g grunt-cli
but now if I try to do grunt init, it is throwing me an error -
A valid Gruntfile could not be found. Please see the getting started
guide for more information on how to configure grunt:
http://gruntjs.com/getting-started Fatal error: Unable to find
Gruntfile.
But when I look inside the grunt folder on my system the Gruntfile.js is there. can someone please guide me how to install this grunt properly and how to write built Script using the grunt. I have one HTML page and java script if i wants built a script using Grunt how can i do it?
To setup GruntJS build here is the steps:
Make sure you have setup your package.json or setup new one:
npm init
Install Grunt CLI as global:
npm install -g grunt-cli
Install Grunt in your local project:
npm install grunt --save-dev
Install any Grunt Module you may need in your build process. Just for sake of this sample I will add Concat module for combining files together:
npm install grunt-contrib-concat --save-dev
Now you need to setup your Gruntfile.js which will describe your build process. For this sample I just combine two JS files file1.js and file2.js in the js folder and generate app.js:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
concat: {
"options": { "separator": ";" },
"build": {
"src": ["js/file1.js", "js/file2.js"],
"dest": "js/app.js"
}
}
});
// Load required modules
grunt.loadNpmTasks('grunt-contrib-concat');
// Task definitions
grunt.registerTask('default', ['concat']);
};
Now you'll be ready to run your build process by following command:
grunt
I hope this give you an idea how to work with GruntJS build.
NOTE:
You can use grunt-init for creating Gruntfile.js if you want wizard-based creation instead of raw coding for step 5.
To do so, please follow these steps:
npm install -g grunt-init
git clone https://github.com/gruntjs/grunt-init-gruntfile.git ~/.grunt-init/gruntfile
grunt-init gruntfile
For Windows users: If you are using cmd.exe you need to change ~/.grunt-init/gruntfile to %USERPROFILE%\.grunt-init\. PowerShell will recognize the ~ correctly.
Some time we need to set PATH variable for WINDOWS
%USERPROFILE%\AppData\Roaming\npm
After that test with where grunt
Note: Do not forget to close the command prompt window and reopen it.
I got the same issue, but i solved it with changing my Grunt.js to Gruntfile.js
Check your file name before typing grunt.cmd on windows cmd (if you're using windows).
You should be installing grunt-cli to the devDependencies of the project and then running it via a script in your package.json. This way other developers that work on the project will all be using the same version of grunt and don't also have to install globally as part of the setup.
Install grunt-cli with npm i -D grunt-cli instead of installing it globally with -g.
//package.json
...
"scripts": {
"build": "grunt"
}
Then use npm run build to fire off grunt.