Order of Execution of packages and script with npm install - node.js

What is the order of execution when we use npm install.
As of now when I use npm install, It looks like-
dev dependencies
preinstall script
dependencies
install
postinstall
But in different condition it behave differently.
What is correct order of Execution?

Related

Using --ignore-scripts for one dependency in NPM

Following this question, NPM dependencies can be installed using:
$ npm install --ignore-scripts
Is there a way to mark that a dependency should be installed without running scripts in package.json?
This is because, when I run npm install --ignore-scripts, the dependency is added to package.json. As a result, other users will install the package while running scripts, however I want this certain package to never run scripts.
I could be wrong but I believe its: npm install -ignore-script package-name#version

Lifecycle of npm install

Npm install executes certain scripts like preinstall , postinstall and others.
I couldn't find explicit list and ordering of these scripts. It would be great to get some clarification and detailed overview of this process.
It is described on the NPM page: npm-scripts.
It is a bit cryptic, but the logic is straightforward.
E.g. running
npm install will do preinstall install postinstall prepublish - this is rather exceptional case, prepublish only runs if there are no arguments, i.e. run locally. Also check which version of npm you are running, as prepublish with local install has been deprecated in 4.x in favour of another approach, described in issue 10074 and has a nice explanation in this blog. It comes down to the fact that npm install without arguments runs when you clone a package and it makes sense to prepare it. But people dislike this behaviour, so it was decided to split prepublish into two stages. prepare runs instead of prepublish during publishing and local npm install. prepublishOnly runs only with npm publish.
Hence
npm publish will do prepublish publish postpublish or prepare prepublishOnly publish postpublish on 4.x
Finally, with version 6.x, npm install runs preinstall install postinstall prepare while npm publish runs prepare prepublish publish postpublish.
As far as I know, all other commands follow the logic of preX, X, postX.

Post install after installing package?

I've got a scripts tag in my package.json file:
"scripts": {
"postinstall": "<command>"
}
Whenever I'm running npm install the postinstall commands runs properly.
But if i'm passing arguments to the npm install command, for example when installing a new package: npm install <dependency> --save-dev. it won't run the postinstall command.
is there a way that the postinstall will run even if there are arguments in the npm install command?
I think you are getting confused about how packages actually work. A package has it's own dependencies, post-installs and pre-installs. When you install a package like this:
npm install <dependency>
This looks at the package you want to install and installs it along with it's dependencies, if the package you want to install has a preinstall or postinstall command, it invokes them. But only for that package, not yours.
When you do npm install, this is installing your package, and it therefore calls your package's postinstall command.

Post processing code after NPM module install

Is it possible to run any post processing script after installing NPM module automatically.
I have a case where, I can install some specific NPM modules in my node.js project. After installation of NPM module, I need to update some places in project automatically.
Is it doable?
Yes. Add a postinstall script to your package.json as described in the npm documentation

How to shrinkwrap devDependencies, but not install them unless necessary?

I have a bunch of devDependencies needed in order to run test suite and have production dependencies locked down with npm shrinkwrap. The problem is that when I run npm install, only production dependencies are installed, in order to install devDependencies, I have to remove npm-shrinkwrap.json and run it again.
Now if shrinkwrap contains devDependencies as well, they get installed in production, where they are not required. Surely there should be some command line arguments to force only normal dependencies to be installed?
September, 2016:
As others have mentioned as well, there were some huge efforts to enhance the shrinkwrap feature starting with npm v3.10.8.
Thanks to this, it'll be possible to keep your devDependencies locked while installing only the production dependencies:
npm shrinkwrap --dev
npm install --only=prod
2013 answer:
As stated in the NPM docs:
Since npm shrinkwrap is intended to lock down your dependencies for
production use, devDependencies will not be included unless you
explicitly set the --dev flag when you run npm shrinkwrap. If
installed devDependencies are excluded, then npm will print a warning.
If you want them to be installed with your module by default, please
consider adding them to dependencies instead.
Basically, or you lock down all deps, or only the production deps.
Not even running npm install --dev or npm install --force can transcend the shrinkwrap functionality.
It looks like this feature was recently added in v3.3 of the npm client per the changelog
You'll now be able to run npm install --only=prod to achieve the effect you wish.
EDIT 2016/09/13
I've tested out npm v3.10.8, and this functionality now works as expected. We've shrinkwrapped our devDependencies and can install only prod dependencies when we deploy.
I think it's worth mentioning that this feature should start working as expected very soon. According to this github issue, tons of people were running into the same problem, and according to this pull request, it will be in the next release (scheduled for 2016-09-08).
With the pull request merged in, all you would have to do is:
npm i --only=prod
As to npm 5 (I've tried on 5.5.1 and 5.6.0), --production (--only=prod) flag is problematic.
When package-lock.json exists in the folder,
npm shrinkwrap --production
simply changes the file name to npm-shrinkwrap.json.
How I managed to solve this issue is to run:
npm prune --production
and then run:
npm shrinkwrap --production
This is fixed in npm 3.10.8; npm install --production shouldn't install dev deps in a shrinkwrap created by npm shrinkwrap --dev: https://github.com/npm/npm/releases/tag/v3.10.8

Resources