How can I update all npm packages/modules at once? - node.js

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?

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 do I update npm in linux?

I've tried the following in order to update npm. But no commands seem to work.
What should I do so I can update npm?
The npm update command updates all packages (no arguments), a specific package (with a package name as argument); in the local directory or in the global modules directory (-g argument).
As stated in the npm documentation, sometimes the npm update npm -g command doesn't work, especially if you run an old version of npm, which you are. To really update npm, run this:
$ curl https://npmjs.org/install.sh | sh

What's an npm command to install devDependencies globally?

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.

npm check and update package if needed

We need to integrate Karma test runner into TeamCity and for that I'd like to give sys-engineers small script (powershell or whatever) that would:
pick up desired version number from some config file (I guess I can put it as a comment right in the karma.conf.js)
check if the defined version of karma runner installed in npm's global repo
if it's not, or the installed version is older than desired: pick up and install right version
run it: karma start .\Scripts-Tests\karma.conf.js --reporters teamcity --single-run
So my real question is: "how can one check in a script, if desired version of package installed?". Should you do the check, or it's safe to just call npm -g install everytime?
I don't want to always check and install the latest available version, because other config values may become incompatible
To check if any module in a project is 'old':
npm outdated
'outdated' will check every module defined in package.json and see if there is a newer version in the NPM registry.
For example, say xml2js 0.2.6 (located in node_modules in the current project) is outdated because a newer version exists (0.2.7). You would see:
xml2js#0.2.7 node_modules/xml2js current=0.2.6
To update all dependencies, if you are confident this is desirable:
npm update
Or, to update a single dependency such as xml2js:
npm update xml2js
To update package.json version numbers, append the --save flag:
npm update --save
npm outdated will identify packages that should be updated, and npm update <package name> can be used to update each package. But prior to npm#5.0.0, npm update <package name> will not update the versions in your package.json which is an issue.
The best workflow is to:
Identify out of date packages with npm outdated
Update the versions in your package.json
Run npm update to install the latest versions of each package
Check out npm-check-updates to help with this workflow.
Install npm-check-updates with npm i npm-check-updates -g
Run npm-check-updates to list what packages are out of date (basically the same thing as running npm outdated)
Run npm-check-updates -u to update all the versions in your package.json (this is the magic sauce)
Run npm update as usual to install the new versions of your packages based on the updated package.json
There is also a "fresh" module called npm-check:
npm-check
Check for outdated, incorrect, and unused dependencies.
It also provides a convenient interactive way to update the dependencies with npm-check -u.
One easy step:
$ npm i -g npm-check-updates && ncu -u && npm i
That is all. All of the package versions in package.json will be the latest major versions.
Edit:
What is happening here?
Installing a package that checks updates for you.
Use this package to update all package versions in your package.json (-u is short for --updateAll).
Install all of the new versions of the packages.
To update a single local package:
First find out your outdated packages by:
npm outdated
Then update the package or packages that you want manually as:
npm update --save <package_name>
This way it is not necessary to update your local package.json
file manually.
Note that the above command will update your package to the latest version.
If you write some version in your package.json file and do:
npm update <package_name>
In this case you will get just the next stable version (wanted) regarding the version that you wrote in your package.json file.
And with npm list <package_name> you can find out the current version of your local package.
You can try either of these options:
Check outdated packages
npm outdated
Check and pick packages to update
npx npm-check -u
No additional packages, to just check outdated and update those which are, this command will do:
npm install $(npm outdated | cut -d' ' -f 1 | sed '1d' | xargs -I '$' echo '$#latest' | xargs echo)
NPM commands to update or fix vulnerabilities in some dependency manifest files
Use below command to check outdated or vulnerabilities in your node modules.
npm audit
If any vulnerabilities found, use below command to fix all issues.
npm audit fix
If it doesn't work for you then try
npm audit fix -f, this command will almost fix all vulnerabilities. Some dependencies or devDependencies are locked in package-lock.json file, so we use -f flag to force update them.
If you don't want to use force audit fix then you can manually fix your dependencies versions by changing them in package-lock.json and package.json file. Then run
npm update && npm upgrade
When installing npm packages (both globally or locally) you can define a specific version by using the #version syntax to define a version to be installed.
In other words, doing:
npm install -g karma#0.9.2
will ensure that only 0.9.2 is installed and won't reinstall if it already exists.
As a word of a advice, I would suggest avoiding global npm installs wherever you can. Many people don't realize that if a dependency defines a bin file, it gets installed to ./node_modules/.bin/. Often, its very easy to use that local version of an installed module that is defined in your package.json. In fact, npm scripts will add the ./node_modules/.bin onto your path.
As an example, here is a package.json that, when I run npm install && npm test will install the version of karma defined in my package.json, and use that version of karma (installed at node_modules/.bin/karma) when running the test script:
{
"name": "myApp",
"main": "app.js",
"scripts": {
"test": "karma test/*",
},
"dependencies": {...},
"devDependencies": {
"karma": "0.9.2"
}
}
This gives you the benefit of your package.json defining the version of karma to use and not having to keep that config globally on your CI box.
As of npm#5.0.0+ you can simply do:
npm update <package name>
This will automatically update the package.json file. We don't have to update the latest version manually and then use npm update <package name>
You can still get the old behavior using
npm update --no-save
(Reference)
A different approach would be to first uprade the package.json file using,
ncu -u
and then simply run,
npm install
to update all the packages to the latest version.
ps: It will update all the packages to the latest version however if the package is already up to date that package will not be affected at all.
3 simple steps you can use for update all outdated packages
First, check the packages which are outdated
sudo npm i -g npm-check-updates
Second, put all of them in ready
ncu -u
Results in Terminal will be like this:
Third, just update all of them.
npm install
That's it.
Just do this to update everything to the latest version -
npx npm-check-updates -u
Note - You'll be prompted to install npm-check-updates. Press y and enter.
Now run npm i. You're good to go.
To really update just one package install NCU and then run it just for that package. This will bump to the real latest.
npm install -g npm-check-updates
ncu -f your-intended-package-name -u
You can do this completely automatically in 2022
Install npm-check-updates
Run the command
ncu --doctor -u
It will first try every dependency you have and run tests, if the tests fail it will update each dependency one by one and run tests after each update
One more for bash:
npm outdated -parseable|cut -d: -f5|xargs -L1 npm i
I'm just interested in updating the outdated packages using the semantic versioning rules in my package.json.
Here's a one-liner that takes care of that
npm update `npm outdated | awk '{print $1}' | tr '\n' ' '`
What it does:
takes the output from npm outdated and
pipes that into awk where we're grabbing just the name of the package (in column 1)
then we're using tr to convert newline characters into spaces
finally -- using backticks -- we're using the output of the preceding steps as arguments to npm update so we get all our needed updates in one shot.
One would think that there's a way to do this using npm alone, but it wasn't here when I looked, so I'm just dropping this here in case it's helpful to anyone πŸ˜€.
** I believe there's an answer that MikeMajara provides here that does something similar, but it's appending #latest to the updated package name, which I'm not really interested in as a part of my regularly scheduled updates.
If you want to upgrade a package to the latest release, (major, minor and patch), append the #latest keyword to the end of the package name, ex:
npm i express-mongo-sanitize#latest
this will update express-mongo-sanitize from version 1.2.1 for example to version 2.2.0.
If you want to know which packages are outdated and which can be updated, use the npm outdated command
ex:
$ npm outdated
Package Current Wanted Latest Location Depended by
express-rate-limit 3.5.3 3.5.3 6.4.0 node_modules/express-rate-limit apiv2
helmet 3.23.3 3.23.3 5.1.0 node_modules/helmet apiv2
request-ip 2.2.0 2.2.0 3.3.0 node_modules/request-ip apiv2
validator 10.11.0 10.11.0 13.7.0 node_modules/validator apiv2
If you have multiple projects with the same node-modules content, pnpm is recommended. This will prevent the modules from being downloaded in each project. After the installation the answer to your question is:
pnpm up

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.

Resources