项目作者: ganesh-tyjo

项目描述 :
NPM Cheat Sheet
高级语言:
项目地址: git://github.com/ganesh-tyjo/npm-cheat-sheet.git
创建时间: 2021-05-23T13:00:59Z
项目社区:https://github.com/ganesh-tyjo/npm-cheat-sheet

开源协议:MIT License

下载


Contributors
Forks
Stargazers
Issues
MIT License
LinkedIn




NPM

NPM Cheat Sheet



This cheat sheet includes commands which will help you while using NPM packages and modules.
Don’t worry about remembering all these NPM commands, Take a look at this cheat sheet whenever required.:wink:
Now go ahead and create something awesome.:sunglasses:



Table of Contents

- Get Version
- Get Help
- Create package.json
- Set Defaults
- Get Defaults
- Remove Defaults
- Install Packages
- Install Certain Version
- Update Packages
- Remove Packages
- Move to Another Folder
- Find Root Folder
- List Packages
- NPM Scripts
- Package Version

Get Version

  1. npm -v
  2. npm --version

Get Help

  1. npm
  2. npm help

Create package.json

  1. npm init
  2. # Below commands will create package.json file with default values
  3. npm init -y
  4. npm init --yes

Set Defaults

  1. npm config set init-author-name 'YOUR NAME'
  2. npm config set init-license 'MIT'

Get Defaults

  1. npm config get init-author-name
  2. npm config get init-license

Remove Defaults

  1. npm config delete init-author-name
  2. npm config delete init-license

Install Packages

Globally

  1. npm install -g package-name

Production dependency

  1. npm install --save package-name

Development dependency

  1. npm install --save-dev package-name

Install Certain Version

Globally

  1. npm install -g package-name@package-version

Production dependency

  1. npm install --save package-name@package-version

Development dependency

  1. npm install --save-dev package-name@package-version

Update Packages

Globally

  1. npm update -g package-name

Production dependency

  1. npm update --save package-name

Development dependency

  1. npm update --save-dev package-name

Remove Packages

Globally

  1. npm uninstall -g package-name

Production dependency

  1. npm uninstall --save package-name

Development dependency

  1. npm uninstall --save-dev package-name

Move to Another Folder

NPM stores installed packages inside node_modules folder.
Most people usually doesn’t share node_modules folder along with their code, because you can easily install all the NPM packages they have used using below commands.

Install production and development dependencies.

  1. npm install

Install production dependencies only.

  1. npm install --production

Find Root Folder

Globally

  1. npm root -g

Locally

  1. npm root

List Packages

Globally

  1. npm list -g
  2. npm list -g --depth 0
  3. npm list -g --depth 1

Locally

  1. npm list
  2. npm list --depth 0
  3. npm list --depth 1

NPM Scripts

Define scripts in package.json file.

  1. "scripts": {
  2. "start": "node index.js",
  3. "script-name":"command-to-run"
  4. }

Run scripts

  1. # Only "start" script will execute without run command
  2. npm start
  3. npm run script-name

Package Version

Find below, What package version with different symbols (*, ~, ^) represents in package.json file.

Version Result
“*“ Install package with latest version
“4.17.3” Install package with exact version
“~4.17.3 Install package with latest patch update (Highlighted part gets updated)
“^4.17.3 Install package with latest minor update (Highlighted part gets updated)

Most Preferred

  1. "package-name": "^4.17.3"