Skip to content

Instantly share code, notes, and snippets.

@erandadev
Last active April 28, 2020 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erandadev/46f227ac6ef60fbe8771e455ccf2c25d to your computer and use it in GitHub Desktop.
Save erandadev/46f227ac6ef60fbe8771e455ccf2c25d to your computer and use it in GitHub Desktop.

NPM Commands and Flags

NPM Commmands

This command used for setup a new npm package. It will create package.jon file

npm init

Install a package

npm install || i <package-name>

Remove a package

npm remove || uninstall || rm || un <package-name>

Update a package

npm update <package-name>

Flags

-y or --yes

When you initialize node package with npm init you have to answer some questions. If you use -y or --yes flag like this npm init -y || --yes you don't have to answer those questions. It will setup the package.json file with default configurations.

npm init -y || --yes

-g or --global

If you want to install a package globally you can use -g or --global like this

npm install -g || --global

What is global means?

When you want to access your packages via terminal or you want to use packages offline you have to install it globally to your computer. It means package you're installing not only accessible in that folder instead it will access everywhere on your computer you don't have to create scripts to run those commands.

-s or --save

When you installing a new package you have to add that package as a dependency into package.json file this flag do it for us.

npm install <package-name>  -s || --save

Note: if you are using current version npm you are no need to add this flag npm install command automatically added that package as a dependency.

--no_save

If you don't want to add you're package as a dependency.

npm install <package-name> --no_save

-D or --save-dev

If you want to install your package as a dev dependency you want to use this flag.

npm install <package-name> --save-dev

--production

Install only Production dependencies

npm install --production

-O or --save-optional

Install package as a optional dependency

npm install <package_name>  --save-optional || -O

@ verion-number

Install specific version of package

npm install <package-name>@<version number>

Ex npm install lodash@1.0.0

Semantic versioning in Packages

Every node package has a version with three numbers separated by "." sign.

  1. The first number represents a major version it can have breaking changes.

  2. The second number represents Minor versions it will add new features to packages and it doesn't break your code most of the time.

  3. The third number represents the patch version. It doesn't include any breaking changes.

Version Symbols

  • * - if you completely remove version number and put '*' it will install the latest version of that package. It is not a good idea because major versions break your code.

  • ^ - if this symbol is added in front of your version number when you run npm install it will install a latest minor version of that package

  • ~ - if this symbol is added in front of your version number when you run npm install it will stick to that minor version and it will only update the patch version.

  • if you put version number without any of those symbols i will install that specific version of that package

Global Modules

Get the installation directory of global packages

npm root -g

Remove Global module

npm un || rm || remove || uninstall -g <package-name>

List your packages

npm list

List your main dependencies

npm list --depth 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment