NPM: Used commands and reference

NodeJS: NPM Most used commands credit to dreamerslab.com

Original URL:

http://dreamerslab.com/blog/en/npm-basic-commands/

The most commonly used npm commands

After setting up n node.js development environment, you need to know some basic commands of node package manager npm. The followings are the most commonly used ones.

Install package globally. Global packages are usually for executable commands.

$ npm install <package name> -g
# example
$ npm install express -g
# now we can use express to generate a new app
$ express new app

Install package locally. Local packages are for the use of require/import in your app/folder.

$ cd /path/to/the/project
$ npm install <package name>
# example
$ npm install express
# now you can use `var express = require( ‘express’ );` in your app

Uninstall global package.

$ npm uninstall <package name> -g
# example
$ npm uninstall express -g

Uninstall local package.

$ cd /path/to/the/project
$ npm uninstall <package name>
# example
$ npm uninstall express

Search package.

$ npm search <package name>
# example
$ npm search express

List global packages.

$ npm ls -g

List global package with depth

$npm ls -g –depth-level=0

List global packages detail.

$ npm ls -gl

List local packages

$ cd /path/to/the/project
$ npm ls

List local packages detail inside local project

$ cd /path/to/the/project
$ npm ls -l

Update global packages.

$ npm update -g

Update local packages.

$ cd /path/to/the/project
$ npm update
Using `package.json` to manage your app packages

With a package.json file in the root of your app dir, you don’t need to manually install every package.

Instead of doing

$ cd /path/to/the/project
$ npm install mongoose
$ npm install express
$ npm install jade

Create a package.json file in the root of your app dir

$ cd /path/to/the/project
$ touch package.json

package.json

{
“name”: “your app name”
, “version”: “0.0.1”
, “private”: true
, “dependencies”: {
“express”: “>=2.5.0”
, “jade”: “>= 0.16.4”
, “mongoose”: “>=2.3.10”
}
}

Then type in the following command all your packages will be installed.

$ npm install -l

Then if you found a package that needs to be in your package.json. Add –save or –save-dev on your “npm install …”.

for eg: “npm install react –save” 

This adds react to your “dependencies” object in your package.json. The other one is –save-dev , the package will be added to your “devDependencies” entry. As you know devDependencies are not outputted in your production ready file, its only for use as you develop. Help us developers a bit 🙂

2 thoughts on “NPM: Used commands and reference”

  1. Hello, i read your blog from time to time and i own a similar one and i was
    just wondering if you get a lot of spam responses?

    If so how do you protect against it, any plugin or anything you can advise?
    I get so much lately it’s driving me crazy so any assistance is very much
    appreciated.

    1. Oh hey dude!

      Thanks for the comment or message, I didnt expect anyone but myself reading my blog lol. Its just a reminder for myself what I have learn towards my career as a developer.

      But anyways! IMO its hard to really 100% secure our site for this kind of thing. Personally, I get one or two on my blog. I would try to put recaptcha on anything coming from your visitor. There is a plugin I used for my company’s website to filter out any comment that might look suspicious. I would just look for “anti-spam” plugin for our wordpress site. Hopefully found a bunch of them. Test them out to see whats work for you.

      I find recaptcha good for everything, cause that way you know which one would really take the time to go through extra step to reach you.
      And they are the one that are actually legit!
      But thats just me!

      Ciao!
      Ernani

Leave a Reply

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