Post install after installing package? - node.js

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.

Related

How to install all relevant packages?

I have a Node.js project which has several dependencies.
I created the package.json file (npm init)
and try to install all the relevant packages
(npm install all) or (npm install)
with npm install I'm getting the following result:
npm notice created a lockfile as package-lock.json. You should commit this file.
but
when I'm trying to run, I'm getting error that a package is missing (i.e express and I need to install it manually)
Is there a way (command) to install at once (with one command) all the relevant packages and dependencies ? (instead of install each of the package manually) ?
Update 1:
I think I found the solution to your problem, you can use this npm module for auto installing your dependencies.
Install
npm install -g auto-install
Usage
Run auto-install in the directory you are working in.
So npm is simple!
you can add a single package using npm install package-name
or
you can install all using npm install, this will read your package.json file and install all the packages which are in there
Additionally you can do this:
npm install package1 package2 package to install multiple packages.
If a required dependency is not installed, just install it once manually using npm's --save argument and it will automatically add that dependency to your package.json.
For instance, if you are missing the express dependency, just run:
npm install --save express
That will install the dependency in your node_modules folder and also automatically update your package.json file to include express. On subsequent installs on different machines, you'll now only need to run npm install and the express package will be added automatically.
Update with a little more context: In Node your package.json file defines all dependencies that are required for your project. However, these dependencies are not automatically generated for you. npm init will create a boilerplate package.json, but it will not fill that package.json with any dependencies.
You need to define the dependencies yourself, which you can do one of two ways:
Manually add lines to your package.json's dependencies section
Use npm install --save <package name> to have npm install a package and automatically save that package as a dependency in your package.json file
If you aren't familiar with package.json files, I'd recommend sticking to approach number 2 so that npm handles editing that file for you.
Once you have a package.json with all of the dependencies you need, then when you pull down your project onto new machines you will be able to automatically install all dependencies at once using a simple npm install command. You can test this on your own machine by deleting the node_modules folder and running npm install, then trying to run your project. If it runs fine then you successfully added all dependencies to your package.json. If it complains about a missing package then add that package with npm install --save <package>

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

Gulp install globally

I need install gulp. I used this command: npm install gulp gulp-plumber gulp-sass gulp-autoprefixer gulp-concat gulp-uglify gulp-notify
Unfortunelly in my project It was created node_modules directory. How do I do this? I don't have to add files in any project. I would like to use only command "gulp watch".
add -g for global installing
$ npm install -g gulp
OR
$ sudo npm install -g gulp
To install a npm package globally on a machine, use one of
npm i -g <packages>
npm install --global <packages>
However, this doesn't let anybody else know about this dependency. Consider instead adding node_modules/ to your .gitignore file (or the ignore of whatever VCS you are using), then install with
npm i --save-dev <packages>
The --save-dev here means that it knows that this dependency is only for development and not required in deployment. So if you use a script to deploy which installs the npm dependencies, you can have it ignore these packages
Add gulp in your package.json and run npm intall from your project directory.

npm install from package.json, the dependancy -v yields command not found

Reinstalled node/npm from scratch and after npm install I can see the node_modules folder with all of the content from package.json. Checking gulp -v gives command not found in the command line on a mac. If I install gulp globally, gulp -v yields the version.
Is there a way of not installing all dependancies globally to use?
Generally gulp is a package which is used to run some task. These task might need to have administrator privileges. So it is better to install it globally using "npm install -g gulp" command. Here -g means "install it globally".
npm install installs the package locally.

npm install vs. update - what's the difference?

What is the practical difference between npm install and npm update? When should I use which?
The difference between npm install and npm update handling of package versions specified in package.json:
{
"name": "my-project",
"version": "1.0", // install update
"dependencies": { // ------------------
"already-installed-versionless-module": "*", // ignores "1.0" -> "1.1"
"already-installed-semver-module": "^1.4.3" // ignores "1.4.3" -> "1.5.2"
"already-installed-versioned-module": "3.4.1" // ignores ignores
"not-yet-installed-versionless-module": "*", // installs installs
"not-yet-installed-semver-module": "^4.2.1" // installs installs
"not-yet-installed-versioned-module": "2.7.8" // installs installs
}
}
Summary: The only big difference is that an already installed module with fuzzy versioning ...
gets ignored by npm install
gets updated by npm update
Additionally: install and update by default handle devDependencies differently
npm install will install/update devDependencies unless --production flag is added
npm update will ignore devDependencies unless --dev flag is added
Why use npm install at all?
Because npm install does more when you look besides handling your dependencies in package.json.
As you can see in npm install you can ...
manually install node-modules
set them as global (which puts them in the shell's PATH) using npm install -g <name>
install certain versions described by git tags
install from a git url
force a reinstall with --force
npm install installs all modules that are listed on package.json file and their dependencies.
npm update updates all packages in the node_modules directory and their dependencies.
npm install express installs only the express module and its dependencies.
npm update express updates express module (starting with npm#2.x, it doesn't update its dependencies).
So updates are for when you already have the module and wish to get the new version.
In most cases, this will install the latest version of the module published on npm.
npm install express --save
or better to upgrade module to latest version use:
npm install express#latest --save --force
--save: Package will appear in your dependencies.
More info: npm-install
npm update: install and update with latest node modules which are in package.json
npm install: install node modules which are defined in package.json(without update)
Many distinctions have already been mentioned. Here is one more:
Running npm install at the top of your source directory will run various scripts: prepublish, preinstall, install, postinstall. Depending on what these scripts do, a npm install may do considerably more work than just installing dependencies.
I've just had a use case where prepublish would call make and the Makefile was designed to fetch dependencies if the package.json got updated. Calling npm install from within the Makefile would have lead to an infinite recursion, while calling npm update worked just fine, installing all dependencies so that the build could proceed even if make was called directly.

Resources