How to change the version of package in package.json - javascript

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

Related

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.
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!

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

Webpack-Cli dependency error in asp.net core 2.0 compilation

I have a asp.net core react app and using webpack for bundling .I have installed required dependencies but when build application it shows an error.
How could i resolve this error.
I have installed required dependencies so many times done npm cache clean and restarted a machine.still its showing not installed in vs solution explorer.
webpack installed in requied folder.
IN vs solution
when try to restore package inside solution explorer,its not installed.
webpack-cli is missing
You doing wrong in your npm install
Should be this command for global
npm i -g webpack-cli
or this command for local
npm i -D webpack-cli
You can not mix up between -D -g flag
If that didn't work for you try to right click on that package and click restore
Update:
Try to delete package-lock.json then run npm i -D webpack-cli again

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.

If babel is already listed in `dev-dependencies` for jest, why do I need to install it separately?

While setting up my newest project, npm seemed to install babel-core and babel-jest automatically on running npm install --save-dev jest.
Since npm's default behavior is to install dev dependencies on installing with --save-dev option, why do the docs specify installing babel again - https://facebook.github.io/jest/docs/en/getting-started.html#using-babel

Categories