How To Use Node Package Manager

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

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

1. Get Current Installed NPM Version.

Execute npm -v to get current installed npm version. If return values then npn is installed, others means npm do not installed.

$ npm -v

2. Update NPM To Newest Version.

For Mac 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

For Windows.

npm install npm -g

3. Install Modules.

$ npm install < module name >

This command will create a node_modules folder under current command execution folder and download and copy module related JavaScript files into node_modules sub folder.

Below is the example result of command $ npm install async.

install node module to current folder

4. Search Node Modules.

If you do not remember the node module name exactly, you can search the module name use below command. This will list all the modules which contains keyword async.

$ npm search keyword ( async )

5. Use The Installed Module.

After install the node module locally, you can use below command to refer to it in node js code.

var async = require('async');

6. Install Node Module Globally.

Below command will install node module in system node module directory. It is something like class path in java.

$ sudo npm install async -g

Above command will install node async module in /usr/local/lib/node_modules folder. Please note to install node module globally you must use root permission. So do not forget use sudo in above command line.

READ :   Node JS UDP Broadcast Example

install node module in global node modules directory

7. Get Installed Module Version.

$ npm list async

Above command will list local installed node async module, it found then display the module path like below.

└── [email protected]
$ npm list express -g

Above command will list express module installed in global directory, if found then return it’s installed directory as below.

└── [email protected]

8. Update Module.

$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.

Uninstall a node module need root permission.

$sudo npm uninstall < module name > ( -g optional )
Uninstall global async module.
$sudo npm uninstall async -g

10. NPM Connection Error.

If you meet below connection errors when operate node modules.

npm err! Error: connect ECONNREFUSED

You can fix above error by below command.

$ npm config set proxy null

11. NPM Permission Error.

When you install, uninstall node module globally, if you meet below error message.

checkPermissions Missing write access to /usr/local/lib/node_modules

That means your current user do not has enough permission to write above directory. So you should su to root user first and them execute the command again. You can also add sudo at the beginning of the command to get root permission.

12. Clear Local NPM Cache.

Below command can avoid publish node js code new version use same old version number.

$ npm cache clear 
$ npm cache verify

13. Unpublish Package With Version Which You Published.

$ npm unpublish <package>@<version>

14. NPM Get Command Help.

Look for help data about how to use npm command.

$ npm help < command > 

15. NPM Package Version Principle.

Package version has three numbers separated by dot such as x.y.z.

  1. 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.
  2. The second number ( y ) is changed when the module add new feature and the feature is backward compatible.
  3. The third number ( z ) is changed for bug fix.
(Visited 209 times, 1 visits today)

Leave a Reply

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.