How To Use Node Package Manager

NPM is the abbreviation of Node Package Manager, it is used to resolve node app code deployment and dependency issues. Commonly it is installed when you install Node. We can use NPM for the below usage.

  1. Download third-party node package that was written by others from NPM repository server to local.
  2. Upload your own package or command-line program to the NPM server for others to use.
  3. Download and install command line programs written by others from NPM server to local.

1. Get The Current Installed NPM Version.

  1. Execute npm -v to get the currently installed npm version.
  2. If the above command returns values then it means NPM is installed, others it means NPM is not installed.
    $ npm -v

2. Update NPM To Newest Version.

  1. For macOS or Linux.
    $ sudo npm install npm -g
    /usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
    /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
    + [email protected]
    added 244 packages, removed 40 packages and updated 121 packages in 28.098s
  2. For Windows.
    npm install npm -g

3. Install Modules.

  1. Run the below command to install a Node.JS module.
    $ npm install < module name >
  2. The above command will create a node_modules folder under the current command execution folder and download and copy module-related JavaScript files into the node_modules subfolder.
  3. Below is the example result of the command to install the Node.JS async module.
    C:\Users\zhaosong> npm install async
    npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\zhaosong\package.json'
    npm notice created a lockfile as package-lock.json. You should commit this file.
    npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\zhaosong\package.json'
    npm WARN zhaosong No description
    npm WARN zhaosong No repository field.
    npm WARN zhaosong No README data
    npm WARN zhaosong No license field.
    + [email protected]
    added 1 package from 1 contributor and audited 1 package in 7.499s
    found 0 vulnerabilities

4. Search Node Modules.

  1. If you do not remember the node module name exactly, you can search the module name use the below command. This will list all the modules which contain the keyword async.
    $ npm search keyword ( async )

5. Use The Installed Module.

  1. After installing the node module locally, you can use the below command to refer to it in the node js source code.
    var async = require('async');

6. Install Node Module Globally.

  1. The below command will install the node module in the system node module directory.
  2. The system node module directory is something like the CLASSPATH in java.
    $ sudo npm install async -g
  3. The above command will install the node async module in the /usr/local/lib/node_modules folder.
  4. Please note to install node module globally you must use root permission.
  5. So do not forget to use the sudo command in the above command line.

7. Get An Installed Node.JS Module Version.

  1. To get the installed Node.JS module version, you can run the below command in a terminal.
    $ npm list async
  2. The above command will list the local installed async node module, if found then display the module path like below.
    └── [email protected]
  3. If you want to get the Node.JS module that is installed in the global directory, you can run the below command.
    $ npm list express -g
  4. The above command will list the Node.JS express module installed in the global directory, if found then return it’s installed directory as below.
    └── [email protected]

8. Update Module.

  1. If you want to update a Node.JS module, you can run the below command.
    $npm update < module name > ( -g optional)
    Update local node module async.
    $npm update async
    Update global node module async.
    $npm update async -g

9. Uninstall Module.

  1. To uninstall a node module you need to have the root user permission.
    $sudo npm uninstall < module name > ( -g optional )
    Uninstall global async module.
    $sudo npm uninstall async -g

10. NPM Connection Error.

  1. If you meet the below connection errors when operating node modules.
    npm err! Error: connect ECONNREFUSED
  2. You can fix the above error by executing the below command.
    $ npm config set proxy null

11. NPM Permission Error.

  1. When you install, uninstall the node module globally, if you meet the below error message.
    checkPermissions Missing write access to /usr/local/lib/node_modules
  2. That means your current user does not has enough permission to write the above directory.
  3. So you should run the su command to change to the root user first and then execute the command again. You can also add sudo at the beginning of the command to get root permission.

12. Clear Local NPM Cache.

  1. Below command can avoid publishing node js code new version use same old version number.
    $ npm cache clear
    $ npm cache verify

13. Unpublish Package With Version Which You Published.

  1. The below command can unpublish the Node.JS package with the specified version you have published.
    $ npm unpublish <package>@<version>

14. NPM Get Help Command.

  1. Look for help data about how to use the npm command.
    $ npm help < command >

15. NPM Package Version Principle.

  1. The Node.JS package version has three numbers separated by dot such as x.y.z.
  2. The first number ( x ) is the big release version, it will be changed if there are big changes in the new release, and do not compatible backward.
  3. The second number ( y ) is changed when the module adds a new feature and the feature is backward compatible.
  4. The third number ( z ) is changed for the bug fix.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.