Summary: NPM common commands and operations

Time:2020-9-26

The full name of NPM is (node package manager), which is a package management and distribution tool installed with nodejs. It is very convenient for JavaScript developersDownload, install, upload and manage installed packages

First of all, the following variables will be used:

  • < name > | < PKG > module name
  • < version > version number
  • < version range > version range
  • < @ scope > scope. All NPM packages have a name. Some package names also have scopes. Detailed interpretation of NPM package name scope / scope|

1、 Install and configure node and preface

#View NPM version 
$NPM - V // 6.4.0

#See the simple usage of each command
$ npm -l 
 
#View NPM command list
$ npm help

#View NPM configuration
$ npm config list -l

2、 NPM init creation module

$ npm init

npm initUsed to initialize and generate a newpackage.jsonDocuments. It will ask users a series of questions. If you don’t want to change the default configuration, just go back all the way.

Fasciculum caudalis-f(for force)-y(for yes), skip the question stage and generate a new one directlypackage.jsonIf there is no suffix, there is a question stage by default.

After the above is created, you can click Package.json You can also modify and update in it.

Summary: NPM common commands and operations

3、 NPM set sets the environment variable

Summary: NPM common commands and operations

$ npm set init-author-name 'my name jerry'
$ set init-author-email '[email protected]'
$ set init-author-url 'http://yourdomain.com'
$ npm set init-license 'MIT'
The above modifications have been implemented, and at this time Package.json It hasn't changed

//After setting, executing init is really successful
$ npm init

Summary: NPM common commands and operations

4、 NPM search module

$NPM search < search term > [- g]

npm searchThe command is used to search the NPM repository. It can be followed by a string or a regular expression.
Summary: NPM common commands and operations

5、 NPM list viewing module

#All modules installed in the current project
$npm list

#It is more concise to list the globally installed modules without going deep into the fulcrum of the package
$ npm list -g --depth 0

4、 NPM install installation module

Basic Usage
#Read package.json Install the configuration sheet inside  
$ npm install 
//It can be abbreviated as NPMI

#The latest (@ latest) version of the specified module is installed by default
$ npm install [<@scope>/]<name> 
//eg:npm install gulp

#Install the specified version of the specified module
$ npm install [<@scope>/]<name>@<version>
//eg: npm install [email protected]

#The specified installation range of the module
$ npm install [<@scope>/]<name>@<version range>
//eg: npm install [email protected]">=1.0.28 < 2.0.0"

#Install the specified tag of the specified module. The default value is (@ latest)
$ npm install [<@scope>/]<name>@<tag>
//eg:npm install [email protected]

#Install via GitHub repository address
$ npm install <tarball url>
//eg:npm install git://github.com/package/path.git
Description of configuration options:
#Global installation
-g | -global 
//Eg: NPMI - G gulp or NPMI gulp - G

#This is the default unless - D or - O exists
#Install and will be added to package.json The dependencies section of.
-P | --save-prod

#**Dependent production stage**
#Install and will be added to package.json The dependencies section of
-S | --save  
//Eg: NPM I gulp -- save or NPM I gulp - S

#**(development phase dependency)**
#Install and will be added to package.json Devdependencies section of.
-D | --save-dev
//NPM I gulp -- save dev or NPMI gulp - D

#**(optional phase dependency)**
#Install and will be added to package.json The optional dependencies section of
-O | --save-optional

#Install the exact version of the module instead of using NPM's default semver range operator
-E | --save-exact
//NPM I gulp -- save exact or NPM I gulp - e
  
#Install and will be added to the 'bundle dependencies' list
-B | --save-bundle
  
#NPM will force the module to be re installed regardless of whether it has been installed or not
-f|--force
//eg:npm install sax --force
//Add: all modules must be forced to be re installed, so delete 'node'_ Modules', re execute 'NPM install'`
$ rm -rf node_ Modules // or manually delete the node_ Modules directory
$ npm install

#Prevent saving to ` dependencies`
--no-save 

#Report installation status rather than actual installation
--dry-run

5、 NPM uninstall module

#Uninstall the current project or global module 
$ npm uninstall <name> [-g] 

eg: npm uninstall gulp --save-dev  
    npm i gulp -g

After uninstalling, you can go to / node\_ Check whether the package still exists in the modules / directory, or use the following command to view it:
NPM LS view installed modules

6、 NPM update module

#Upgrade the specified module of the current project or global
$ npm update <name> [-g] 
//eg: npm update express 
      npm update express -g

7、 NPM link reference module

#Reference dependency some packages are installed globally, so you only need to reference them in the project.
$ npm link [<@scope>/]<pkg>[@<version>]
//Eg: reference NPM link gulp gulp SSH gulp FTP
//Eg: dereference NPM unlink gulp

#The reference module I used less, did not say in depth, when the use can go to the document examples
$ npm link (in package dir)

8、 NPM run execution script

package.jsonOfscriptsField, which can be used to specify script commands fornpmCall directly.npm runA shell is created to execute the specified command.

The two commands are abbreviated,startandtestAs a special command, run can be omitted and the rest must be carried with run.
npm runParameters of.
  • If you run directly without any parameters, thepackage.jsonAll script commands that can be executed
    Summary: NPM common commands and operations
  • The configurable parameter format is to add two hyphens (- -)

    --- package.json Documents---
    "scripts": {
      "test": "mocha test/"
    }
    
    -------Terminal-------
    $ npm run test -- anothertest.js
    #Equivalent to direct execution
    $ mocha test/ anothertest.js
Internal variables
--- package.json Documents---
{
    "name": "npm_test",
    "version": "1.0.0",
    "config": {
        "reporter": "xunit"
    },
    "script":{
        "bundle": "mkdir -p build/$npm_package_version/",
        //$npm_ package_ Version reads the value of the outer "version", the same as $NPM_ package_ Name reads the outer "name" value
        "test": "mocha test/ --reporter $npm_package_config_reporter"
        //$npm_ package_ config_ Reporter reads "reporter" in "config"
    }
}
pre-andpost-Two hooks

If there are two hooks in pretlm, they will be checked first
Execute firstNPM run pre - command name, and then executeNPM run command name, and finally executeNPM run post - command name

--- package.json Documents---
"scripts": {
    "lint": "eslint --cache --ext .js --ext .jsx src",
    "test": "karma start --log-leve=error karma.config.js --single-run=true",
    "pretest": "npm run lint",
    "posttest": "echo 'Finished running tests'"
  }
  
-------Terminal-------
$ npm run lint
//Execute NPM run lint directly to end

$ npm run test
//Because two hooks, pretest and posttest, are defined.
//So NPM run pretest is executed first
//Then execute NPM run test
//Finally, execute NPM run posttest

9、 NPM publish publishing module

#Unregister apply to register a user directly in https://www.npmjs.com/ Registration is the same
$ npm adduser
//Fill in the following questions: username, password and email

#Registered
$ npm login 

#Release
$ npm publish

Martfor your reference only, please correct and add thanks


Data:
Ruan Yifeng https://javascript.ruanyifeng…
Official website document: https://www.npmjs.com.cn/
Rookie tutorial: https://www.runoob.com/nodejs…
Detailed explanation of common commands of white oak NPM
NPM releases a package for beginners
How to publish your own NPM package (module)?

Recommended Today

On the theoretical basis of SRE

What is SRE? When I first got into contact with SRE, many people thought that it was a post with full stack capability in Google and could solve many problems independently. After in-depth exploration, it is found that SRE can solve many problems, but there are too many problems. It is difficult for a post […]