npm install behavior after clear cache vs folder delete - node.js

I am trying to understand how npm work on cache clean and installation. Whenever I have to get latest latest version I try to do:
$ npm cache clean
$ npm install
But it fails to pickup latest dependencies so I always have to do delete old dependent project folder from node_module folder and do npm install
So I want to understand why?

npm cache clean only purges the global cache npm uses to avoid re-downloading the same packages. npm install does nothing if the installed package versions match the versions in package.json.
Maybe the command you're looking for is npm update?

Related

NPM, force reinstall of a local package

I have an angular application that uses a local external package packaged with
npx webpack && npm pack
that creates a [package]-[version].tgz and installs in the main application with
npm i [../relative/path/to]/[package]-[version].tgz
The problem is, everytime I make a change to the package, to make NPM install the new version I have to change the version on the package, effectively swapping everytime from version A and version B, otherwise the changes are ignored.
Is there a way to force NPM to reinstall the package without changing version? I tried every combination I could find on the net (even npm uninstall [package] && npm cache clear --force && npm i [../relative/path/to]/[package]-[version].tgz --force && npm cache clear --force) to no avail.
Node is v14.19.1, NPM is 6.14.16

Install npm dependencies for specific node version using package-lock.json

On my local environment, I'm using the latest node version but during the deployment of my AWS Lambda function I have to use v8.10.0.
I have created .nvmrc file with v8.10.0 set and to install node_modules I simply run:
nvm exec npm i
The problem that it is not using package-lock.json at all - which is even altered by npm i command.
node v8.10.0 delivers npm v5.6.0 which doesn't provide npm ci command.
Is there any other way to install dependencies for v8.10.0 using lock file?
Unfortunately, in versions of npm after 5.0 (specifically, 5.4 and onward), "npm install" can update the package lock file, and there is no option to avoid this behavior.
In npm >=6.0, you can use npm ci, which installs only from the package lock file, which is what you want. (See https://github.com/npm/npm/issues/17979 and https://github.com/npm/npm/issues/18286 for a lot of backstory/history.)
If you can use node v8.14 instead of v8.10, you'll get npm#6 out of the box. If not, you might be up the creek.
Have you tried npm shrinkwrap ? IIRC package-lock.json has been changed to npm-shrinkwrap.json, but I can't remember in which version
That is, run npm install && npm shrinkwrap and make sure the resulting json gets committed into your repo / transferred to AWS

gulp 4.0.0 shows npm outdated

I am running the latest available Gulp.js 4.0.0 and yet somehow npm says this:
> npm outdated
Package Current Wanted Latest Location
gulp 4.0.0 4.0.0 3.9.1
To solve the issue I have done:
npm update
npm update -g
remove node_modules and run npm install lependu answer
remove node_modules, npm cache clean -f and run npm install lependu answer
Is there a way to fix this?
npm had some issues with publication of new packages this week and the week before too. See this So I guess an npm cache clean -f would help. If not, you can do nothing more, than wait.

How to get npm 5 to update lock file after adding new save-dev dependency?

When running npm install redux-observable --save-dev with npm 5.0.3 in an existing project with an existing package.json, the lock file gets updated with the newly installed package (redux-observable in this case), but not any existing packages in the package.json. Running npm install to try and refresh or regenerate the lock file doesn't work.
This is a known bug in npm 5.0.x:
https://github.com/npm/npm/issues/16893
To work around this issue, you must run npm install with no arguments in a freshly cloned/created repository before doing npm install --save-dev package-name. If you get the ordering wrong, you'll need to delete the lock file and then re-run npm install in order to get npm to add entries for the existing package.json to the lock file.

What NPM command should users run if the package.json file has been updated?

If I update the package.json file in an NPM workflow app, what command do existing users run to update their local node_modules dependencies?
To start using it, they run:
$ npm install
So what do they run if there is a change to the package.json file? Or do they just delete the folder and re-run the npm install command?
To re-validate the package.json and install adjusted versions or new packages:
$ npm install
The one thing this won't do is remove packages that aren't in package.json. To do that, run:
$ npm prune
If you've only changed package versions and not added new packages:
$ npm update
If you you've updated a specific package version:
$ npm update {packagename}
You should either do
npm install && npm prune
or
npm upgrade && npm prune
npm install will be faster than npm upgrade because it only updates packages in node_modules if package.json demands a newer version. npm uprade, on the other hand, will download updates to dependencies if they are available, which may include bug fixes. For ≥npm-5, you should use npm install because npm upgrade will have the side-effect of modifying any package-lock.json file which should not be modified unless if you are the package’s maintainer.
npm prune is necessary because the updates to package.json may have removed dependencies. If you do not run npm prune, packages installed by a prior version of package.json will remain in the node_modules directory that would not be there if you freshly downloaded/cloned the project and ran npm install. Due to how some packages conditionally call require() or even scan the node_modules directory, leaving packages which were removed from package.json can result in unexpected behavior.

Resources