Install, update, and uninstall NPM packages

Table of contents

1. Download and install the global package

2. Solve the EACCES permission error when installing packages globally

2.1 Reinstall NPM

2.2 Manually change the default directory of npm

3. Update packages downloaded from the registry

3.1 Update local packages

3.2 Updating globally installed packages

3.3 Determine which global packages need to be updated

3.4 Updating a single global package

3.5 Update all globally installed packages

4. Use NPM packages in the project

4.1 Using unscoped packages in your project

Node.js modules

package.json file

4.2 Using scoped packages in your project

Node.js modules

package.json file

5. Using deprecated packages

6. Uninstall packages and dependencies

6.1 Uninstall local packages

Remove local packages from node_modules directory

unscoped package 

 scope package 

6.2 Delete local package without deleting from package.json

6.3 Uninstall global packages

unscoped package 

scope


1. Download and install the global package

Installing a package globally allows you to use the code in the package as a set of tools on your local computer.

To download and install packages globally, run the following commands on the command line:

npm install -g <package_name>

Tip: If you're using npm 5.2 or higher, we recommend running npxpackages globally.

If you get EACCES permission errors, you may need to reinstall npm using a version manager, or manually change npm's default directory.

2. Solve the EACCES permission error when installing packages globally

If you
see EACCESerrors when trying to install a package globally, you can:

  • Reinstall npm using node version manager (recommended)
  • Manually change the default directory for npm

 Generally, it is caused by system directory permissions. For example, there may be such problems in the system disk (C disk). At this time, we can install it under other disks other than C disk (for example: in the D disk directory) to solve it. .

2.1 Reinstall NPM

You don't need to remove the current version of npm or Node.js before installing Node Version Manager.

The installation method can refer to the installation of Node.js

2.2 Manually change the default directory of npm

Note: This section is not applicable to Windows system, it is best to reinstall Windows and change the installation directory.

1. Back up the original npm global directory first.

2. On the command line, in your home directory, create a directory for the global installation:

mkdir ~/.npm-global

 3. Configure npm to use the new directory path:

npm config set prefix '~/.npm-global'

4. In your favorite text editor, open or create a ~/.profilefile and add the following lines:

export PATH=~/.npm-global/bin:$PATH

5. On the command line, update the system variables:

source ~/.profile

6. To test your new configuration, install sudoa package globally without using:

npm install -g jshint

You can use the corresponding ENV variable (eg: if you don't want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global

Finally, if you don't want to reinstall the original package, you can also copy the previous package to the current directory.

3. Update packages downloaded from the registry

Updating local and global packages downloaded from the registry helps keep code and tools stable, available, and secure.

3.1 Update local packages

We recommend regularly updating the local packages your project depends on so that your code improves as its dependencies improve.

1. Change to the root directory of the project and make sure it contains package.jsonthe files:

cd /path/to/project

2. In the project root directory, run the update command:

npm update

3. To test the update, run the outdated command
. There should be no output.

npm outdated

3.2 Updating globally installed packages

Note: If the version of npm you are using is too low, an error will be reported when updating the package. It is recommended to update to a new version of npm, and then update all outdated global packages.

Please consider upgrading to the latest version of npm:

npm install npm@latest -g

3.3 Determine which global packages need to be updated

To see which global packages need updating, run on the command line:

npm outdated -g --depth=0

3.4 Updating a single global package

To update a single global package, run on the command line:

npm update -g <package_name>

3.5 Update all globally installed packages

To update all global packages, run on the command line:

npm update -g

4. Use NPM packages in the project

Once you node_modulesinstall a package in
, you can use it in your code.

4.1 Using unscoped packages in your project

Node.js modules

If you're creating a Node.js module, you can use a package in your module by passing it as an argument to a requirefunction.

var deepMerge = require('deepmerge')
var test = deepMerge.all([[10, 20, 30], [40, 50]])
console.log('test: ', test);
// test:  [ 10, 20, 30, 40, 50 ]

package.json file

In package.json, list the packages under dependencies. You can optionally include semantic versioning
.

{
  "dependencies": {
    "package_name": "^1.0.0"
  }
}

4.2 Using scoped packages in your project

To use a scoped package, simply include the scope anywhere the package name is used.

Node.js modules

var projectName = require("@scope/package-name")

package.json file

In package.json:

{
  "dependencies": {
    "@scope/package_name": "^1.0.0"
  }
}

5. Using deprecated packages

If you install a package and it prints a deprecation message, we recommend that you follow the instructions (if possible). This might mean updating to a new version, or updating package dependencies.

A deprecation message does not always mean that the package or version is not available; it may mean that the package is not maintained and will no longer be updated by the publisher. 

6. Uninstall packages and dependencies

If you no longer need a package in your code, we recommend that you uninstall it and remove it from your project's dependencies.

6.1 Uninstall local packages

Remove local packages from node_modules directory

To remove a package from the node_modules directory, use the uninstall command on the command line
. Include scope if package has scope.

This will uninstall a package, completely removing everything npm installed on its behalf. It also removes packages from the dependencies, devDependencies, optionalDependencies and peerDependencies objects in package.json. Also, if you have npm-shrinkwrap.json or package-lock.json, npm will also update those files.

unscoped package 

npm uninstall <package_name>

 scope package 

npm uninstall <@scope/package_name>

6.2 Delete local package without deleting from package.json

Using --no-savewill tell npm not to remove packages from the package.json, npm-shrinkwrap.jsonor files.package-lock.json

--saveor -Swill tell npm to remove the package from the package.json, npm-shrinkwrap.jsonand package-lock.jsonfiles. This is the default, but you may want to use this option if you save=falsehave e.g..npmrc

6.3 Uninstall global packages

To uninstall an unscoped global package, use the command with the uninstallflag on the command line -g. Include scope if package has scope.

unscoped package 

npm uninstall -g <package_name>

scope

npm uninstall -g <@scope/package_name>

Guess you like

Origin blog.csdn.net/u014388408/article/details/132159664