A brief introduction to NPM

1. NPM -v, NPM version

View the installed version of NPM

2. npm init

This command generates a package.jsonwenjian file. This file is the description file of the entire project. From this file, you can clearly know your project’s package dependencies, version, author information, and so on. Each NPM package has its own package.json file.

3.npm install

Run this command to install the package. Add the –save or -s parameter after the command to record the package installation information in the Dependencies field of the package.json file. This makes it easy to manage package dependencies. If the package is only needed at the development stage, you can go ahead and add the -dev parameter so that information about installing the package is recorded in the devDependencies field of the package.json file.

4. Package. The json file

This file is the file that provides the package description. In Node.js, a package is a folder in which package.json files store descriptions of the package in JSON format. All the fields in the file description: docs.npmjs.com/cli/v7/conf…

Ii. Module loading principle and loading method

1. Require import modules

Modules in Node.js can be divided into sound modules and file modules. Modules can be imported from Node.js using the require method and exported from exports method. For a native module (such as HTTP), simply import the module using require(‘ HTTP ‘) and assign it to a variable to use the attributes, methods (that is, functions) exported by the module. For file modules, you can use a “./ “prefix to refer to the current path, thus loading the module using a relative path. When loading a module, you can omit the.js extension. For example, there is a file module named mymodule.js in the node folder of the same class, which can be imported like this:

const myModule = require('./node/myModule');
Copy the code

A sibling of the node_modules folder can be loaded like this:

const underscore = require('./underscore');
Copy the code

This is because node.js automatically finds and loads modules in the node_modules folder.

2. Exports module

Variables and methods in a module can only be used by that module. If you want to share methods, attributes, etc., with other modules, you can export an object with exports that contains methods, attributes, etc., that you want to share with other modules.

#require import file vs. import import file

Import is an ES6 syntax that must be converted to es5 syntax in order to be compatible with browsers. Import is called at compile time, so it must be placed at the beginning of a file. Assigning the result of require to a variable import is a deconstruction process, but none of the engines currently implement import. We use Babel in Node to support ES6, which is simply transcoding ES6 to ES5. Import syntax is transcoding to require links: https://www.jianshu.com/p/0fb49a748f80Copy the code

Import Usage www.jb51.net/article/180…