What's an npm command to install devDependencies globally? - node.js

I'd prefer to type a short command, like npm install -g, to setup a project's global dependencies, such as node-sass and jshint, than manually typing out npm install -g every single package. Is there an npm-idiomatic way to do this?

You are using npm install -g <pkg> wrong here. -g indicates, that it's no project dependencies, than rather you global (PC wide).
Those plugins are no devDependencies, but CLI runners. What you want is npm install --save-dev every single package upon initialisation. When you need to install those dependencies again you'd just run npm install and include something like ./node_modules/.bin/jshint to your package.json scripts in order not to depend on the CLIs.

I get you are not supposed to do it, but if you still want it, change your global location to make sure you have permissions. Install jq (either download it, or apt install jq) and then:
export NPM_CONFIG_PREFIX=~/npm-global
cat ./package.json | jq '.devDependencies | keys[] as $k | "\($k)#\(.[$k])"' | xargs -t npm install --global
This will create a list of packages and versions from the devDependencies section, pipe that into xargs, and call npm install with them.

Related

How to uninstall NPM modules from the devDependencies in node.js?

How can I uninstall npm modules with devDependencies in Node.js?
Use command:
1)npm uninstall <name of the module>
Also you can use:
1) npm uninstall <name of the module>: to remove the module from node_modules, but not package.json
2) npm uninstall <name of the module> --save: to also remove it from dependencies in package.json
3) npm uninstall <name of the module> --save-dev: to also remove it from devDependencies in package.json
4) npm -g uninstall <name of the module> --save: to remove it globally
For dev dependencies you can preform one -of- two commands, depending on your situation.
If you simply want to remove the dependency you can use the following.
npm rm name-of-dependency
TIP: If you forget how the name was spelled, check your package.json file under "devDependencies".
If you installed the dependency as a "dev-dependency", and you decided after the fact that you wanted it installed as a regular "dependency" then you can simply install it using the -S flag, as shown below:
npm i -S name-of-dependency
TIP: It also works the other way around. To move a dependancy from the "dependencies" field in your package.json file, to the "devDependencies" field, swap out the -S flag for the -D flag.
The install command i and the rm command (also the -S and -D flags) are currently the method that NPM uses to document the process for removing packages, and or changing a packages dependency type.
SEE MORE ABOUT NPM CLI COMMANDS FOR INSTALLING & REMOVING PACKAGES HERE

How can I update all npm packages/modules at once?

I'm struggling to find a way to update all npm packages in one go, some articles suggest that package.json file should be edited where all version numbers need to be changed to * therefore forcing node to grab latest versions, but others state that such method is not considered good. Ideally, I want to find a command line option for this.
One simple step:
$ npm i -g npm-check-updates && ncu -u && npm i
This will install ncu, use it set all of your packages in package.json to the latest version and finally apply the updates.
npm outdated is the command that you want to run to find all of the packages that are not up-to-date. You could pipe the output of npm output -json into a file and then iterate over the JSON to install the latest versions of the packages.
You can try these one-liners.
Update all dependencies:
$ npm out --long --parseable |grep 'dependencies$' |cut -d: -f4 |xargs npm install --save
Update all devDependencies:
$ npm out --long --parseable |grep 'devDependencies$' |cut -d: -f4 |xargs npm install --save-dev
Keep in mind though that this is not usually a good idea as you might have to change something in the process of upgrading a package. If your project has many dependencies it is better to update them one by one or in small groups and run tests frequently.
For a single module you could try npm install --save module#latest That would change package.json too. You could write a shell script or script in nodejs to iterate though package.json and update all of the modules.
Recursive update of all modules can performed with npm update:
for locally installed modules: npm update --depth 9999 --dev
for globally installed modules: npm update --depth 9999 --dev -g
A ready-to-use NPM-script to update all Node.js modules with all its dependencies:
How to update all Node.js modules automatically?

Switch versions of node.js packages

I installed two different versions of a node package:
npm install sails -g
npm install sails#beta -g
What could I use to use these versions according what is specified in package.json in a directory basis?
First of all, when you are doing
npm install sails -g
npm install sails#beta -g
it installs the packages globally, and the second command will override the first. One of the main purposes of global installs is having the executable command (sails in our case) available in the PATH. And this command, basically, defines for which version of Sails you will generate the new application when you type sails new ....
Long story short, if you really need to be able to use two different versions of the package, you can install one of them locally and then provide the full path to the executable. Something like:
npm install sails -g
mkdir -p ~/tmp
cd ~/tmp
npm install sails#beta
cd ~/Sites
sails new thisWillBeAStableApp
../tmp/node_modules/.bin/sails new thisWillBeABetaApp
The generators are supposed to configure package.json files accordingly.

How can I uninstall npm modules in Node.js?

As commonly known, any npm module can be installed by running a simple command: npm install <module_name>.
I have installed a few modules that I do not use any more and I just want to get them off. I have a few questions regarding this:
Do we have any command or process to uninstall a module from the root (something like npm uninstall <module_name>)
or will simply removing the module files do?
How does it affect us if we keep the unused modules?
The command is simply npm uninstall <name>
The Node.js documents https://npmjs.org/doc/ have all the commands that you need to know with npm.
A local install will be in the node_modules/ directory of your application. This won't affect the application if a module remains there with no references to it.
If you're removing a global package, however, any applications referencing it will crash.
Here are different options:
npm uninstall <name> removes the module from node_modules but does not update package.json
npm uninstall <name> --save also removes it from dependenciesin package.json
npm uninstall <name> --save-dev also removes it from devDependencies in package.json
npm uninstall -g <name> --save also removes it globally
If it doesn't work with npm uninstall <module_name> try it globally by typing -g.
Maybe you just need to do it as an superUser/administrator with sudo npm uninstall <module_name>.
Well, to give a complete answer to this question, there are two methods (for example we call the installed module as module1):
To remove module1 without changing package.json:
npm uninstall module1
To remove module1 with changing package.json, and removing it from the dependencies in package.json:
npm uninstall --save module1
Note: to simplify the above mentioned commands, you can use -S instead of --save , and can use remove, rm, r, un, unlink instead of uninstall
I just install stylus by default under my home dir, so I just use npm uninstall stylus to detach it, or you can try npm rm <package_name> out.
To uninstall the Node.js module:
npm uninstall <module_name>
This will remove the module from folder node_modules, but not from file package.json. So when we do npm install again it will download the module.
So to remove the module from file package.json, use:
npm uninstall <module_name> --save
This also deletes the dependency from file package.json.
And if you want to uninstall any globally module you can use:
npm -g uninstall <module_name> --save
This will delete the dependency globally.
To remove packages in folder node_modules in bulk, you could also remove them from file package.json, save it, and then run npm prune in the terminal.
This will remove those packages, which exist in the file system, but are not used/declared in file package.json.
P.S.: This is particularly useful on Windows, as you may often encounter problems with being unable to delete some files due to the "exceeded path length limit".
Sometimes npm uninstall -g packageName doesn’t work.
In this case you can delete package manually.
On Mac, go to folder /usr/local/lib/node_modules and delete the folder with the package you want. That's it. Check your list of globally installed packages with this command:
npm list -g --depth=0
You can also run the following as shorthand:
npm un packageName or npm rm packageName
Note: Add -g at end of command to uninstall global packages.
Update for npm 5:
As of npm 5.0.0, installed/uninstalled modules are added/removed as a dependency by default, so the --save option is no longer needed.
Run
npm uninstall <package>
For example:
npm uninstall mongodb
It will remove the module from the node_modules folder and also the package.json file.
Alias can be used to to uninstall node_modules package
un alias for uninstall
removes single package
- npm un <PACKAGE_NAME>
removes multiple packages by adding space between packages names
- npm un <PACKAGE_NAME_1> <PACKAGE_NAME_2>
removes all node_modules packages
- rm -rf node_modules/
I found this out the hard way, even if it is seemingly obvious.
I initially tried to loop through the node_modules directory running npm uninstall module-name with a simple for loop in a script. I found out it will not work if you call the full path, e.g.,
npm uninstall module-name
was working, but
npm uninstall /full/path/to/node_modules/module-name
was not working.
For Windows users - if you want to remove all the Node.js modules installed at once:
Open a PowerShell window
Go inside the node_modules folder (cd node_modules)
Run this command - "npm uninstall (Get-ChildItem).Name"
It will uninstall all the modules.
To uninstall a module using npm, you can use:
npm uninstall moduleName
Also, if you want to uninstall and want the change to be reflected in your package.json then you can use the --save flag, like this:
npm uninstall moduleName --save
OR
npm uninstall -S
And if you want to uninstall a module from devDependencies and want the change to be reflected in package.json then you can use -D flag, like this:
npm uninstall moduleName -D
The uninstall option didn't work for me when I tried to use the same command to the one I used in installing (as I was installing with the #latest directive)
So for example, I installed a package like this:
npm install #ngtools/webpack#latest
And then I wanted to uninstall it, so I used the same command (including #latest):
npm uninstall #ngtools/webpack#latest
So the above uninstall didn't work. I have to remove the #latest, and then it worked well:
npm uninstall #ngtools/webpack
In npm v6+ npm uninstall <package_name> removes it both in folder node_modules and file package.json.
Simply,
npm un module_name
OR
npm uninstall module_name
Make sure you uninstall it in the same directory as the package.json and node_modules folder.
Additionally, if you've started using yarn, in place of npm:
yarn remove <package-name>
Is the equivalent of:
npm uninstall <package-name> --save
This will
- remove the package from package.json, as well as
- uninstall it from your project's node-modules folder
# Log in as root (might be required depending on install)
su -
# List all global packages
npm ls -g --depth=0
# List all local (project) packages
npm ls -p --depth=0
# Remove all global packages
npm ls -g --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm
# Remove all local packges
npm ls -p --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -p rm
# NOTE (optional): to use node with sudo you can add the bins to /usr/bin
# NOTE $PATHTONODEINSTALL is where node is installed (e.g. /usr/local/node)
sudo ln -s $PATHTONODEINSTALL/bin/node /usr/bin/node
sudo ln -s $PATHTONODEINSTALL/bin/npm /usr/bin/npm
The simplest solution is:
npm uninstall packageName --save-dev
See upper level packages names in the your project:
npm list --depth=0
The output will be like:
app#0.1.0 /home/jackkobec/projects/myAppName
├── packageName#packageVersion
├── express#4.16.4
Copy package name and execute npm uninstall command. Example for express package:
npm uninstall express --save-dev
If you want to uninstall an specific package using npm,
you can use the following command :
Sintax:
npm uninstall <package-name>
Example:
npm uninstall moment
If you want to uninstall a number of modules, then just run the npm uninstall.
Then go to file package.json and delete the unwanted module from there, and then just run the command npm install. It should fix your problem.
In case you are on Windows, run CMD as administrator and type:
npm -g uninstall <package name>
You can delete a Node.js module manually. For Windows,
Go to the node_modules directory of your repository.
Delete the Node.js module you don't want.
Don't forget to remove the reference to the module in your package.json file! Your project may still run with the reference, but you may get an error. You also don't want to leave unused references in your package.json file that can cause confusion later.

How do you prevent install of "devDependencies" NPM modules for Node.js (package.json)?

I have this in my package.json file (shortened version):
{
"name": "a-module",
"version": "0.0.1",
"dependencies": {
"coffee-script": ">= 1.1.3"
},
"devDependencies": {
"stylus": ">= 0.17.0"
}
}
I am using NPM version 1.1.1 on Mac 10.6.8.
When I run the following command from the project root, it installs both the dependencies and devDependencies:
npm install
I was under the impression that this command installed the devDependencies:
npm install --dev
How do I make it so npm install only installs dependencies (so production environment only gets those modules), while something like npm install --dev installs both dependencies and devDependencies?
The npm install command will install the devDependencies along other dependencies when run inside a package directory, in a development environment (the default).
In version 8.x and above use --omit=dev flag to install only regular dependencies:
npm install --omit=dev
This will install only dependencies, and not devDependencies, regardless of the value of the NODE_ENV environment variable.
If you use 6.x or an earlier version, you need to use the --only=prod flag instead.
Note:
Before v3.3.0 of npm (2015-08-13), the option was called --production, i.e.
npm install --production
You may also need --no-optional flag.
I run into that problem too! npm install is somewhat confusing and web posts keep bringing in the -d/--dev flags as if there is an explicit 'development' install mode.
npm install will install both "dependencies" and "devDependencies"
npm install --production will only install "dependencies"
npm install --dev will only install "devDependencies"
The new option is:
npm install --only=prod
If you want to install only devDependencies:
npm install --only=dev
If you have already installed all your dependencies, and you want to avoid having to download your production packages from NPM again, you can simply type:
npm prune --production
This will remove your dev dependencies from your node_modules folder, which is helpful if you're trying to automate a two step process like
Webpack my project, using dev dependencies
Build a Docker image using only production modules
Running npm prune in between will save you from having to reinstall everything!
If you read this POST in 2016, please achieve what you want by using
--only={prod[uction]|dev[elopment]}
argument will cause either only devDependencies or only non-devDependencies to be installed regardless of the NODE_ENV.
from: https://docs.npmjs.com/cli/install
When using "npm install" the modules are loaded and available throughout your application regardless of if they are "devDependencies" or "dependencies". Sum of this idea: everything which your package.json defines as a dependency (any type) gets installed to node_modules.
The purpose for the difference between dependencies/devDependencies/optionalDependencies is what consumers of your code can do w/ npm to install these resources.
Per the documentation: https://npmjs.org/doc/json.html...
If someone is planning on downloading and using your module in their
program, then they probably don't want or need to download and build
the external test or documentation framework that you use.
In this case, it's best to list these additional items in a
devDependencies hash.
These things will be installed whenever the --dev configuration flag
is set. This flag is set automatically when doing npm link or when
doing npm install from the root of a package, and can be managed like
any other npm configuration param. See config(1) for more on the
topic.
However, to resolve this question, if you want to ONLY install the "dependencies" using npm, the following command is:
npm install --production
This can be confirmed by looking at the Git commit which added this filter (along with some other filters [listed below] to provide this functionality).
Alternative filters which can be used by npm:
--save => updates dependencies entries in the {{{json}}} file
--force => force fetching remote entries if they exist on disk
--force-latest => force latest version on conflict
--production => do NOT install project devDependencies
--no-color => do not print colors
#dmarr try using npm install --production
npm will install dev dependencies when installing from inside a package (if there is a package.json in the current directory). If it is from another location (npm registry, git repo, different location on the filesystem) it only installs the dependencies.
I suggest to use npm ci. If you want to install only production-needed packages (as you wrote - without devDependencies) then:
npm ci --only=production
or
NODE_ENV=production npm ci
If you prefer oldschool npm install then:
npm install --production
or
NODE_ENV=production npm install
Here is good answer why you should use npm ci.
It's worth mentioning that you can use the NODE_ENV environment variable to achieve the same result. Particularly useful if you're containerizing your Node application (e.g. Docker).
NODE_ENV=production npm install
The above code will install all your dependencies but the dev ones (i.e. devDependencies).
if you need to use environment variables in your Dockerfile more information can be found here.
Environment variables are easy to overwrite whenever needed (e.g. if you want to run your test suite say on Travis CI). If that were the case you could do something like this:
docker run -v $(pwd):/usr/src/app --rm -it -e NODE_ENV=production node:8 npm install
NPM Documentation here
production
Default: false
Type: Boolean
Set to true to run in "production" mode.
devDependencies are not installed at the topmost level when running local npm install without any arguments.
Set the NODE_ENV="production" for lifecycle scripts.
Happy containerization =)
npm install --production --no-optional
It installs only deps from dependencies and will ignore optionalDependencies and devDependencies
Use npm install packageName --save this will add package in dependencies, if you use npm install packageName --save-dev then it devDependencies.
npm install packageName --save-dev should be used for adding packages for development purpose. Like adding TDD packages (Chai, mocha, etc). Which are used in development and not in production.
I have found that, when trying to install dev dependencies for a package that contains a node addon, you cannot avoid building the addon when running npm install --dev even if you just want to install the devDependencies. So, I had to go around npm's back:
node -e 'console.log( Object.keys( require( "./package.json" ).devDependencies ) );' | \
sed -e "s/^[^']*'//" -e "s/'.*$//" | \
xargs npm install
Or, better (and more succinctly) yet,
node -e 'Object.keys( require( "./package.json" ).devDependencies )
.map( function( item ){ console.log( item ) } );' | xargs npm install
I ran into a problem in the docker node:current-slim (running npm 7.0.9) where npm install appeared to ignore --production, --only=prod and --only=production. I found two work-arounds:
use ci instead (RUN npm ci --only=production) which requires an up-to-date package-lock.json
before npm install, brutally edit the package.json with:
RUN node -e 'const fs = require("fs"); const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8")); delete pkg.devDependencies; fs.writeFileSync("./package.json", JSON.stringify(pkg), "utf-8");'
This won't edit your working package.json, just the one copied to the docker container.
Of course, this shouldn't be necessary, but if it is (as it was for me), there's your hack.
Need to add to chosen answer: As of now, npm install in a package directory (containing package.json) will install devDependencies, whereas npm install -g will not install them.
npm install --production is the right way of installing node modules which are required for production. Check the documentation for more details
Now there is a problem, if you have package-lock.json with npm 5+. You have to remove it before use of npm install --production.

Resources