Let's say, I have install some new packages, uninstall some packages, update some packages to new versions. All changes are saved into package.json. When I pull the changes with this new package.json, is there a single command to do the synchronisation between the locally installed packages and those specified in the updated package.json?
I am looking for something like:
$ npm syncrhonise
npm install will install all packages specified in package.json. npm update will do the same but will also go a find any new versions of those packages. You can uninstall an individual package using npm uninstall <package>. There are already solutions to remove all packages here
I found two ways:
First npm prune will uninstall everything not listed in your package.json
npm prune [<name> [<name ...]]
This command removes "extraneous" packages. If a package name is
provided, then only packages matching one of the supplied names are
removed.
Extraneous packages are packages that are not listed on the
parent package's dependencies list.
Documentation available at prune.
Second You could remove your node_modules/ folder and then reinstall the dependencies from package.json.
rm -rf node_modules/
npm install
This would erase all installed packages in the current folder and only
install the dependencies from package.json. If the dependencies have
been previously installed npm will try to use the cached version,
avoiding downloading the dependency a second time.
Windows Trick
Due to its folder nesting Windows can’t delete the folder as its name is too long. To solve this, install RimRaf:
npm install rimraf -g
rimraf node_modules
Related
I am trying to do local development of an NPM package and test it in a package which depends on it. I'm using NPM (7.5.3) and specifically npm link for this but running into a problem with the chain of dependencies.
The child package has dependencies, these are all added to the parent's node_modules folder when using npm install "git+https://github.com/name/child_package". But when I npm link that module:
cd child_package
npm link
cd ../parent_package
npm link child_package
With the last command run (npm link child_package), all of the dependencies for child_package which were in the node_modules of parent_package are removed. NPM reporting:
removed 60 packages, changed 1 package, and audited 231 packages in 1s
At which point all the compilation in the parent package fails due to the missing deps. It finds the child_package, which is symlinked as expected, but dependency defined in child_package of "gsap" has now been removed.
If I reinstall it using npm install "git+https://github.com/name/child_package" it will add the deps back into the node_modules folder of the parent project.
try to do the following:
cd child_package
npm install
that will install child dependencies to directory of child package
personally I hate npm link and always use npm publish (use version number like 1.0.0-preview.1 for your child package and remove '-preview.Number' when you are done)
This is a behavior introduced in npm V7 + .
The only reasonable "workaround" i have found is to go back to npm 6 (npm install -g npm#6).
Another "workaround" is to npm install --no-save ../../my-local-module but to reflect changes to the local module you will need to delete it from node_modules and reinstall again. Kind of lame....
I have tried to remove a package by using
npm uninstall (package_name) -s
Which removed the package from package.json but didn't remove it from node_modules folder, so how can I remove these unused package from node_modules folder?
Edit:
I am sharing this Q&A as I faced this issue personally, having used all variations of npm uninstall but it didn't remove the package from node_modules, so I shared what helped me remove about 10 unused packages which was npm prune
As per the npm guide, npm uninstall removes the package from dependencies only, it will not remove the package folder from node_modules (that is not mentioned in the description anyway)
For some reason I thought the npm uninstall will remove the package folder from node_modules too, but it didn't happen, after some research I found that we should use
npm prune
This command will remove unused packages from node_modules automatically as per the official npm description
that the npm uninstall will only remove the package from your
package.json file but it will not delete the package from your node_modules
I like to share my struggle in uninstalling a package from my project. I believe it may add value to the answers above.
I evaluated a package named react-router in one of my projects. Later I decided to remove it.
I wanted to remove the package first and then remove its reference from my code.
I tried npm uninstall and npm prune. However, in both cases, it failed to remove the package from my node-modules folder.
I removed the reference of react-router from the code and tried again. I still couldn't remove it with the above command.
After some digging, I found that I also installed react-router-dom that uses the package react-router. So I had to remove it first using the commands above. Only then, I was able to remove the react-router.
I find it strange that npm uninstall does not remove packages if they are referred. I think, if it allows, I don't have to manually find the usage of the package by searching them in class instead of IDE/Lint can help me find it much more easily.
If you tried both npm prune and npm uninstall --save but the modules still exist in node_modules, try to remove the node_modules folder:
rm -rf node_modules
and then run
npm install
to rebuild your node_modules again with only the modules you need
I've been using ember for a while and when I wanted to install the node dependencies of a project, I just needed to use npm install to create the folder node_modules with all the dependencies (as it's described in http://ember-cli.com/user-guide/).
Since I was using an old version of node I unisntalled node and npm and installed nvm with the versions node v5.0.0 and npm v3.3.6but now, when I try to use npm install to install the dependencies of a project as I used to do before, instead of the dependencies of the package.json file, I get many, many more from things I'm not sure where they come (I think they are dependencies that npm handles by itself in a globally way but now it's adding them to my project locally, but I'm not sure).
Why am I getting all those unknown (for me) dependencies?
Notice that, when I run ember new it generates the correct dependencies in node_modules but if I delete this folder and run npm install happens the same.
That's one of the changes introduced by npm v3.0:
Your dependencies will now be installed flat - by default. If
possible, all of your dependencies, and their dependencies, and their
dependencies will be installed in your project's node_modules folder
without nesting. Nesting will only occur when two or more modules have
conflicting dependencies.
Read more at http://www.felixrieseberg.com/npm-v3-is-out-and-its-a-really-big-deal-for-windows/
Is there a way to see what packages are installed in node_modules that aren't in package.json? I know that npm-check and dependency-check can be used to check modules currently in package.json, but I wonder if there is a way to look for packages that might just be extraneously installed.
Use npm list or its shortcut npm ls to list installed packages.
Packages that not in the package.json will be marked as extraneous.
There is also npm ll or npm la which lists extended information about packages.
First experiences with node.js/npm. From the npm-install docs I read:
npm install takes 3 exclusive, optional flags which save or update the package version in your main package.json:
--save: Package will appear in your dependencies.
--save-dev: Package will appear in your devDependencies.
--save-optional: Package will appear in your optionalDependencies.
But I can't understand how it works in practice. If, for example, I run the command:
npm install bower --save-dev
I'd expect to find a package.json file in the current directory with devDependencies set to the installed version of bower, instead I find nothing.
Am I doing/expecting something wrong?
Using node v0.10.21, npm 1.3.12 on Ubuntu 12.04 x64
npm won't create package.json for you, but it will create the necessary dependencies for you as long as package.json exists and is legal JSON.
Create it like so
echo {} > package.json
Then, doing npm i --save whatever will add whatever#~x.x.x as a dependency as expected. The file needs to be there, and be JSON, that's it.
npm install only fetches the packages from the registry and puts them in your ./node_modules. It updates your package.json to register this new dependency if you tell it to.
Your package.json has three dependency blocks :
dependencies - these are needed for your app to run.
devDependencies - these are needed for the developer environments for your app (this is how your teammates can get the packages that you recently added to the project. The dependencies listed here are not required on production hosts.)
optionalDependencies - These packages are optional and it is OK if npm cant reolve the package to install. i.e a build failure does not cause npm install to fail (Note however, that your app should handle cases where the package cannot be found.)
Here is the behavior with the different usages of the npm install command:
$ npm install async #Only installs, no change made to package.json
$ npm install async --save #Installs, adds async#version to dependencies block
$ npm install async --save-dev # Installs, adds async#version to the devDependencies block
$ npm install async --save-optional # Installs, adds async#version to the optionalDependencies block