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

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.

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:
ncu Checks for updates from the package.json file
ncu -u Update the package.json file
npm update --save Update your package-lock.json file from the package.json file

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.

To update the version in the package.json the shortest approach is to uninstall and install the package -
npm uninstall name_of_package
and
npm install name_of_package --save
Enjoy!

Related

i can't install any node packages with npm install

I try to install node packeges with nmp. When i run, it says:
up to date, audited 356 packages in 7s
found 0 vulnerabilities
I see it as a dependency in my package-json like this:
"dependencies": {
"express": "*",
"nodemon": "*"
}
but there is no node_modules was installed..
Hope someone can help.
edit: I tried the commands given in the answers but it didn't work. still same
ss from editor
These commands solve the same problem and have a look at the attached links.
https://github.com/npm/npm/issues/17282
Can't install anything using "npm install"
npm install -g npm
npm cache clean
npm update
npm install
There are a few fixes to this issue.
The first one is to install the dependencies manually, as so.
$ npm install -s express nodemon
The command should install express and nodemon at the latest versions.
The second option is to run a few commands, as so (in the order given).
$ npm install -g npm
$ npm cache clean --force
$ npm update
$ npm install
Basically, these are what the commands do (from 1-4).
This installs npm from npm (sounds weird, but there is an npm package).
This command forcefully cleans out the cache of npm.
As you can tell, npm update updates npm!
Finally, this command should be run in a directory where there is a package.json file. This installs all the dependencies listed in package.json.
Don't type out the $ in the terminal examples!

Can't npm install local dependency

I've been using npm install react-financial-charts successfully. However, I want to include this package locally instead (for reasons), so I checked out the master branch of react-financial-charts from Github. I now have two folders:
C:\Users\user\projects\react-financial-charts // fresh checkout from Github
C:\Users\user\projects\myproject // my project
Inside of my project, my package.json contains:
"dependencies": {
"react-financial-charts": "file:C:/Users/user/projects/react-financial-charts"
}
npm run dev will now encounter the compile error corresponding to a basic import statement import { BarSeries } from "react-financial-charts" in one of my files:
Module not found: Error: Can't resolve 'react-financial-charts' in 'C:\Users\user\projects\myproject\src\App'
So basically, the simple import statement which used to work (when I was doing npm install react-financial-charts), is now no longer working when I install the dependency from a local folder instead.
EDIT: I also tried these things that an answer below suggested, but I'm getting the exact same error message:
npm link ../react-financial charts
npm install ../react-financial charts
npm install --save ../react-financial charts
EDIT 2: This ended up working, thanks to the suggested answer below. The trick was I needed to npm update and npm install inside the dependency before linking.
cd react-financial-charts
npm link
cd ../myproject
npm link react-financial-charts
Method 1: Using npm-link
Go to C:\Users\user\projects\react-financial-charts in terminal:
npm link
Now, go to your project C:\Users\user\projects\myproject:
npm link react-financial-charts
Now, any changes to C:\Users\user\projects\react-financial-charts will be reflected in C:\Users\user\projects\myproject. Note that the link should be to the package name, not the directory name for that package.
Method 2: Saving local repo as npm-install
npm install --save ../path/to/mymodule

package-lock.json not updated after removing a package from package.json?

If i manually add a package to package.json and then run npm install, my package-lock.json gets updated with that new package's dependencies.
However, if i then manually delete that package from package.json from npm install, that package's dependencies are not removed from package-lock.json.
So - package-lock.json only gets modified when adding/updating packages in package.json? Not when removing?
This is a known issue with npm.
See issue :package-lock.json file not updated after package.json file is changed
" For now I'm working around it by changing my npm install command to rm -f package-lock.json && npm install. Obviously, that's not an elegant solution, and somewhat defeats the purpose of having a lockfile in the first place."
rm -f package-lock.json && npm install
This is supposed to be fixed in : https://github.com/npm/npm/pull/17508
See article : https://medium.com/coinmonks/everything-you-wanted-to-know-about-package-lock-json-b81911aa8ab8 for a better understanding.
Removal of package-lock.json should be only done in case of corrupted lock file. To remove package you should just use npm cli (it will update lock file)
npm uninstall <package>
This is a good explanation. https://stackoverflow.com/a/54127283/5040275
From the NodeJs docs
The package-lock.json sets your currently installed version of each package in stone, and npm will use those exact versions when running npm install.
NPM by default will read the package-lock.json file.
Therefore, in the first scenario, npm is reading the package entry from package.json since that's the only file which has an entry of the particular package.
Whereas in the second scenario, it is reading it from package-lock.json, as is its default behaviour

How to upgrade React

I've understood that I'm using react.js version 15.6.2. I know this because doing this in my code:
console.log(React.version);
results in this console output:
15.6.2
I want to upgrade to React#^v16.2.0. I tried doing this:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo n latest
But nothing changes. I get the same version output in the console. How do I upgrade node?
EDIT:
Here's the situation, I'm in a project folder with the following hierarchy:
node_modules seems to contain the react installation, since it has a react folder with a package.json file containing the version number 15.6.2.
I've tried both npm update --save react and npm update -g react. None worked. Same thing happens, and the same version number can be found in node_modules/react/package.json. I even tried to run npm install again before hosting with npm start. Any other suggestions?
Use npm update --save react to update to latest version.
For a specific version use npm update --save react#16.2.0.
React should not be installed globally but only for your project. If this is not the case use -g instead of --save
UPDATE
Okay my fault. Just use npm install --save react#16.2.0. This installs the new version.

How to change the version of package in package.json

If I uninstall a package from my project like this:
npm uninstall react
the line related to this package in package.json file does not disappear, and then when I install a different version of this package, like this:
npm install react#15.0.0
the package gets installed to the node_modules, but the version in package.json remains to be unupdated, meaning that it is still the old version of a package, that I had before the uninstallation.
How to delete / update / change the version in package.json through terminal?
You need to add --save to the command in both install and uninstall cases.
This way, when uninstalling with --save, the package's line will be erased from package.json as well as from node_modules. And when installing with --save, the package's line will be added to the package.json, as well as the node_modules.
So, you should go like that, for example:
npm uninstall react --save
npm install react#15.0.0 --save

Categories