NPM (Node Package Manager) is a Package management and distribution tool that comes with NodeJS, making it easy for JavaScript developers to download, install, upload, and manage installed packages.

Here are a few variables that will be used:

  • | module name
  • The version number
  • Version range
  • <@scope> Scope. All NPM packages have a name. Some package names also have scopes.

A,This section describes how to install and configure Node

$NPM -l $NPM -l $NPM -l $NPM -l $NPM -l $NPM -l $NPM -l $NPM -l list -lCopy the code

NPM init create module

NPM init is used to initialize a new package.json file. It asks the user a series of questions, and if you don’t want to change the default configuration, you can just press enter.

If -f (for force) and -y (for yes) are appended, the question stage is skipped and a new package.json file is generated directly. If no suffix is appended, the question stage is created by default.

Once the above is created, you can view it directly in package. json and modify it in it.

NPM set sets environment variables

$ 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' does not change package. jsonCopy the code

NPM Search module

The NPM search command is used to search the NPM repository. It can be followed by either a string or a regular expression.

NPM list check module

$NPM list -g --depth 0 list of all modules installed in the current projectCopy the code

NPM install NPM install

Basic usage

$NPM install [<@scope>/]<name> //eg: NPM install [<@scope>/ $NPM install [<@scope>/]<name>@<version> //eg: $NPM install [<@scope>/]<name>@<version range> //eg: NPM install vue@">=1.0.28 < 2.0.0" # $NPM install [<@scope>/]<name>@<tag> //eg: NPM install [email protected] # by making installation code base address $NPM install < tarball url > / / eg: NPM install git://github.com/package/path.gitCopy the code

Description of configuration options:

# global – g | – global/installation/eg: NPM gulp or NPM I gulp I – g – g # this is the default setting, unless – or – O # D installation and will be added to the package. The json dependencies of the area. – P | – save – prod # dependence (production) * * * * # installation and will be added to the package. The json dependencies area – S | – save / / eg: NPM I gulp –save or NPM I gulp -s #** (development-phase dependencies) ** # install and will be added to the devDependencies section of package.json. – D | – save – dev / / NPM I gulp – save – dev or NPM gulp – I # D * * * * # dependence phase (optional) is installed and will be added to the package. The json optionalDependencies area – O | –save-optional # Install the exact version of the module, Rather than using the default semver NPM range operator – E | – save – exact / / NPM I gulp – save – exact or NPM I gulp – E # installation and will be added to the ` bundleDependencies ` list – B | – save – whether to install a bundle # module, NPM are forced to install – f | — force / / eg: NPM install sax — force / / added: To force all modules to be reinstalled, remove ‘node_modules’, Re-execute ‘NPM install’ rm -rf node\_modules _// or manually delete the node\_modules directory _npm install # to prevent saving to ‘dependencies’ –no-save # Report installation status instead of actual installation –dry-run

NPM uninstall Uninstall modules

$NPM uninstall <name> [-g] eg: NPM uninstall gulp --save-dev NPM I gulp -g Once uninstalled, you can go to /node\_modules/ to see if the package still exists, or use the following command: NPM ls To check the installed moduleCopy the code

NPM update module

$NPM update <name> [-g] //eg: NPM update express NPM update express-gCopy the code

NPM link reference module

Some packages are installed globally, and only references are required within the project. $NPM link [<@scope>/]< PKG >[@<version>] //eg: $NPM link (in package dir) $NPM link (in package dir)Copy the code

NPM run execute script

The scripts field of package.json can be used to specify script commands to be invoked directly by NPM. NPM run creates a Shell that executes the specified command.

Two command abbreviations,startandtestThis is a special command, so you can omit run, and everything else must be run.

npm runThe parameters.

  • If run without any arguments, it will be listedpackage.jsonAll script commands that can be executed

  • Configurable parameter format is with two conjunction lines (–)

    – package. The json file — — — “scripts”: { “test”: “mocha test/” } — — — — — — — end — — — — — — – NPM run test _\– anothertest.js_ _\# is equivalent to executing _ directly mocha test/ anothertest.js

Internal variables

- package. The json file - {" name ":" npm_test ", "version" : "1.0.0", "config" : {" reporter ":" xunit} ", "script" : {" bundle ": $npm_package_version = $npm_package_version = $npm_package_version $npm_package_name = test; $npm_package_name = test; $npm_package_config_reporter $npm_package_config_reporter $npm_package_config_reporterCopy the code

pre-andpost-Two hooks

NPM checks to see if the prelint and postlint hooks are defined, and if so, NPM run pre-name, then NPM run post-name, and finally NPM run post- name.

- package. The json file - "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 // Directly run NPM run lint to end $NPM run test // There are two hooks defined pretest and posttest. NPM run pretest // Then NPM run test // finally NPM run posttestCopy the code

NPM publish module

# unregistered apply to register a user directly at https://www.npmjs.com/ like $NPM adduser // After executing fill in a few questions Username, Password, Email # registered $NPM login # publish $ npm publishCopy the code

Thanks for your help

This article uses the article synchronization assistant to synchronize