preface

Today, with the rise of all three frameworks, we install Node.js and use its package management tool (NPM). Although usually we only need to be able to install, start and pack, but we also need to know more about it.

Template to load

Core module loading

Core modules are modules that come with Node.js. When Node starts, the core module file is already compiled into the binary file, so we just need to load it by name.

It’s worth noting that the Node.js platform is not technically running web-side javaScript, but rather “javaScript” implemented by EcmaScript on the Node.js platform. The “javaScript” running on node.js platform does not have BOM and DOM, but has more server-side apis, such as the ability to manipulate files and HTTP services.

Loading third-party modules (usually used)

The third party module here is what we normally download via NPM, for example: you want to download a time formatting plugin (moment.js). You NPM install moment, the moment plugin is a third party module.

Loading mode:

  1. Start by finding the node_modules directory in the directory where the current file resides
  2. Then locate the main property in the file based on package.json(not the outer layer) in the file directory
  3. The entry module to moment.js is recorded in the main property
  4. The third party package is then loaded, but the file is actually loaded
  5. If the package.json file does not exist or the entry module specified by main does not exist
  6. Node will automatically look for index.js in that directory, which means that index.js is a default option
  7. If none of the above conditions is true, the node_modules directory in the directory above is searched
  8. If the previous level does not exist, the search continues one level up
  9. Can not find module XXX if the root directory of the current disk cannot be found

So we usually use the global directive, for example: NPM install moment –global. It will be in the disk root directory.

Common command

Initialize the

// Skip the wizard and quickly generate a package.json file in the current directory NPM init -y or --yesCopy the code

The installation

NPM install Packagename 0.0.1 // The latest version is installed by default The --save and -s parameters save the module version information to dependencies. NPM install packagename --save -- S // --save-dev Save the module version information to devDependencies. NPM install packagename --save-dev -d // Install packagename -- devDependencies NPM install Packagename - g or - globalCopy the code

To view

// View all installed modules NPM list or NPM ll or NPM la or NPM ls NPM ls -g --depth 0 // View the current installed package version NPM ls packagename // View the global installed package version NPM ls packagename -g // View all version information NPM view Packagename versions // View the latest version information. NPM view Packagename VersionCopy the code

update

// Update modules can also be used (up, upgrade). Of course, you can specify the version, specify whether it is global, and specify whether it is a development dependency. npm update [name [version]][-g][--save][--save-dev]]Copy the code

uninstall

// Uninstall the installed module. You can also use remove, rm, r, UN, unlink. Of course, you can specify the version, specify whether it is global, and specify whether it is a development dependency. npm uninstall [name [version]][-g][--save][--save-dev]]Copy the code

The cache

NPM cache clean --force NPM cache clean --forceCopy the code

Package. json and package-lock.json function analysis

  • The package.json file contains the dependencies installed by the project. When node_modules is removed, you can use this file to install the dependencies you need.
  • Package-lock. json file was not included in NPM 5 before, it was added after NPM 5.
  • When installed, NPM generates or updates the package-lock.json file.
  • Versions after NPM 5 do not require the –save parameter when installing packages, it will automatically save the dependency information.
  • The package-lock.json file is automatically created or updated when the package is installed.
  • The package-lock.json file will hold all the package information (version, download address) in node_modules, which will be faster when NPM install is restarted. It is mainly used to lock the version number to prevent the automatic upgrade of the new version, resulting in compatibility problems.

The source operation

Some addresses are easy to wall, so we need to perform source cutting operation.

CNPM (China Node Package Manager)

npm install cnpm -g --registry=https://registry.npm.taobao.org
Copy the code

However, this method is not recommended. CNPM has some bugs according to the contents reflected online (the details are not clear), but it is still recommended to use Taobao mirror.

nrm

NRM is basically a source cutting tool that records most of the images and can manage its own image addresses (you can add or remove them).

npm install -g nrm
Copy the code

Specific commands:

NRM ls // Mirror list NRM -h // View all commands NRM test // Test the mirror speed NRM use Taobao // Use taobao mirror NRM add registry HTTP: / / http://registry.npm.frp.trmap.cn/ / / increase source NRM del < registry > / / delete the sourceCopy the code