NPM, force reinstall of a local package - node.js

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

Related

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

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.

"libsass bindings not found. Try reinstalling node-sass"

So today I was trying to install MeepBot for StreamMe, and I ran into the error that says: "'libsass' bindings not found. Try reinstalling 'node-sass'." I reinstalled it like a million times. I have tried: "npm un/install --save-dev node-sass," "npm rebuild node-sass," etc. Can someone please give me an answer to my problem?
Picture: http://prntscr.com/axbxu8
P.S. keep in mind, I am using CentOS 6.
According to node-sass project's README.md only binaries for "popular platforms"(i.e. Windows/Mac) are included and you may need to build for other platforms like CentOS.
Here are roughly the steps (reading the readme would give you a better idea):
- cd to the node-sass directory within your project source.
- node scripts/install.js
- node scripts/build.js
Should see a message like Binary is fine; exiting.
Try to run npm rebuild node-sass again and it should work!
have you tried reinstalling everything? rm -rf node_modules; npm i. i've had to do that multiple times before.
otherwise, npm rebuild node-sass should work unless there are multiple versions of node-sass in your dependency tree - then maybe not. are you using npm v3+?
I would suggest try upgrading your gcc compiler as node-sass uses gcc to compile. And then try this -
npm rebuild node-sass
If that doesn't work then try runing this code (you must be using node version 4 or above).
npm install -g n
rm -R node_modules/
npm uninstall --save-dev node-sass
npm install --save-dev node-sass#2
npm install
npm -g install node-gyp#3
npm rebuild node-sass
If you are using multiple version of node then you will have to run npm rebuild node-sass every time you change node version.
You can also use gulp-sass npm install gulp-sass#2 if you want.
I was facing this issue. In my case the parent package.json was referring to new version of node-sass but the version of gulp-sass was referring to old node-sass. As soon as I updated gulp-sass to latest version, and ran 'npm rebuild node-sass', issue was gone.

npm install behavior after clear cache vs folder delete

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?

npm install vs. update - what's the difference?

What is the practical difference between npm install and npm update? When should I use which?
The difference between npm install and npm update handling of package versions specified in package.json:
{
"name": "my-project",
"version": "1.0", // install update
"dependencies": { // ------------------
"already-installed-versionless-module": "*", // ignores "1.0" -> "1.1"
"already-installed-semver-module": "^1.4.3" // ignores "1.4.3" -> "1.5.2"
"already-installed-versioned-module": "3.4.1" // ignores ignores
"not-yet-installed-versionless-module": "*", // installs installs
"not-yet-installed-semver-module": "^4.2.1" // installs installs
"not-yet-installed-versioned-module": "2.7.8" // installs installs
}
}
Summary: The only big difference is that an already installed module with fuzzy versioning ...
gets ignored by npm install
gets updated by npm update
Additionally: install and update by default handle devDependencies differently
npm install will install/update devDependencies unless --production flag is added
npm update will ignore devDependencies unless --dev flag is added
Why use npm install at all?
Because npm install does more when you look besides handling your dependencies in package.json.
As you can see in npm install you can ...
manually install node-modules
set them as global (which puts them in the shell's PATH) using npm install -g <name>
install certain versions described by git tags
install from a git url
force a reinstall with --force
npm install installs all modules that are listed on package.json file and their dependencies.
npm update updates all packages in the node_modules directory and their dependencies.
npm install express installs only the express module and its dependencies.
npm update express updates express module (starting with npm#2.x, it doesn't update its dependencies).
So updates are for when you already have the module and wish to get the new version.
In most cases, this will install the latest version of the module published on npm.
npm install express --save
or better to upgrade module to latest version use:
npm install express#latest --save --force
--save: Package will appear in your dependencies.
More info: npm-install
npm update: install and update with latest node modules which are in package.json
npm install: install node modules which are defined in package.json(without update)
Many distinctions have already been mentioned. Here is one more:
Running npm install at the top of your source directory will run various scripts: prepublish, preinstall, install, postinstall. Depending on what these scripts do, a npm install may do considerably more work than just installing dependencies.
I've just had a use case where prepublish would call make and the Makefile was designed to fetch dependencies if the package.json got updated. Calling npm install from within the Makefile would have lead to an infinite recursion, while calling npm update worked just fine, installing all dependencies so that the build could proceed even if make was called directly.

Resources