Jamal Ahmed
Jamal Ahmed

Reputation: 667

npm update is not updating the version in package.json file

I am trying to update specific package in my project. I have checked it using npm outdated and then I run this command to update this package: npm update nameofpackage i.e., npm update slugify.

My package.json file is not got updated after that, although when i run npm outdated again it shows no outdated package. It means it got updated but my package.json file still shows the older version of the package.

Please let me know how can I update my package.json file also. I have tried npm update slugify --save also but it didn't worked for me.

Upvotes: 45

Views: 49801

Answers (2)

csmaster
csmaster

Reputation: 963

Simple and Recent:

install ncu package globally:

npm install -g npm-check-updates

Then these two following code:

Check for the latest update:

If you want to update all dependencies regardless of the minor or major update limit (~ , ^ ) in the package.json file run the following code to check first for the available update options:

$ ncu
[====================] 4/4 100%

 react          16.8.6  →  18.2.0
 react-dom      16.8.6  →  18.2.0
 react-scripts   3.2.0  →   5.0.1
 typescript      3.3.3  →   4.9.4

Update package.json file:

Run ncu -u to upgrade package.json.

Note: the npm update only checks for updates respecting semver (see the documentation here.

ncu -u

Then run npm update command to update package-lock.json and optimize dependency update in package.json.

npm update --save

Finally, run the following command to install packages:

npm install

Note: ncu only update the package.json file.

Upvotes: 12

Antonin
Antonin

Reputation: 695

The objective of the npm update command is to update your package-lock.json according to what you have specified in the package.json file. This is the normal behavior.

If you want to update your package.json file, you can use npm-check-updates: npm install -g npm-check-updates.

You can then use these commands:

  1. ncu Checks for updates from the package.json file
  2. ncu -u Update the package.json file
  3. npm update --save Update your package-lock.json file from the package.json file

Upvotes: 56

Related Questions