https://nodejs.dev/learn/npm-global-or-local-packages
In general, all packages should be installed locally.
npm global or local packages
The main difference between local and global packages is this:
- local packages are installed in the directory where you run
npm install <package-name>
, and they are put in thenode_modules
folder under this directory - global packages are all put in a single place in your system (exactly where depends on your setup), regardless of where you run
npm install -g <package-name>
In your code you can only require local packages:
JScopy
require('package-name')
so when should you install in one way or another?
In general, all packages should be installed locally.
This makes sure you can have dozens of applications in your computer, all running a different version of each package if needed.
Updating a global package would make all your projects use the new release, and as you can imagine this might cause nightmares in terms of maintenance, as some packages might break compatibility with further dependencies, and so on.
All projects have their own local version of a package, even if this might appear like a waste of resources, it's minimal compared to the possible negative consequences.
A package should be installed globally when it provides an executable command that you run from the shell (CLI), and it's reused across projects.
You can also install executable commands locally and run them using npx, but some packages are just better installed globally.
Great examples of popular global packages which you might know are
npm
create-react-app
vue-cli
grunt-cli
mocha
react-native-cli
gatsby-cli
forever
nodemon
You probably have some packages installed globally already on your system. You can see them by running
BASHcopy
npm list -g --depth 0
on your command line.
TEST
npm list -g --depth 0
/Users/spiffy/.nvm/versions/node/v14.16.1/lib
├── @aws-amplify/cli@4.50.2
├── bower@1.8.12
├── codesandbox@2.2.3
├── ghost-cli@1.17.3
├── grunt-cli@1.4.3
└── npm@6.14.8
npm ERR! missing: winston-transport@https://github.com/winstonjs/winston-transport/archive/868d6577956f82ee0b021b119a4de938c61645f7.tar.gz, required by winston-daily-rotate-file@4.5.4
MORE NPM COMMMANDS
https://docs.npmjs.com/uninstalling-packages-and-dependencies
If you no longer need to use a package in your code, we recommend uninstalling it and removing it from your project's dependencies.
Uninstalling local packages
Removing a local package from your node_modules directory
To remove a package from your node_modules directory, on the command line, use the uninstall
command. Include the scope if the package is scoped.
Unscoped package
npm uninstall <package_name>
Scoped package
npm uninstall <@scope/package_name>
Example
npm uninstall lodash
Removing a local package from the package.json
dependencies
To remove a package from the dependencies in package.json
, use the --save
flag. Include the scope if the package is scoped.
Unscoped package
npm uninstall --save <package_name>
Scoped package
npm uninstall --save <@scope/package_name>
Example
npm uninstall --save lodash
Note: If you installed a package as a "devDependency" (i.e. with --save-dev
), use --save-dev
to uninstall it:
npm uninstall --save-dev package_name
Confirming local package uninstallation
To confirm that npm uninstall
worked correctly, check that the node_modules
directory no longer contains a directory for the uninstalled package(s).
- Unix system (such as OSX):
ls node_modules
- Windows systems:
dir node_modules
Uninstalling global packages
To uninstall an unscoped global package, on the command line, use the uninstall
command with the -g
flag. Include the scope if the package is scoped.
Unscoped package
npm uninstall -g <package_name>
Scoped package
npm uninstall -g <@scope/package_name>
Example
For example, to uninstall a package called jshint
, run:
npm uninstall -g jshint
Resources
Uninstalling local packages
Uninstalling global packages
-
I also ran the new version without reading the comments. ALWAYS READ THE COMMENTS. Here is how to restore NPM: curl npmjs.org/install.sh | sh – Jack Allan Jul 8 '14 at 22:12
• Warning: the new version doesn't filter out the npm module. You don't want to remove that one. – neverfox Mar 20 '14 at 6:06
The following command removes all global npm modules. Note: this does not work on Windows. For a working Windows version, see Ollie Bennett's Answer.
npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm
Here is how it works:
npm ls -gp --depth=0
lists all global top level modules (see the cli documentation for ls)awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}'
prints all modules that are not actually npm itself (does not end with/npm
)xargs npm -g rm
removes all modules globally that come over the previous pipe