The module specification is the basis for building a large Node.js application, so it is very important; The Node.js module specification is also known as the CommonJS module specification.

CommonJS module specification

Previously the only way to load JS files was through the

  • If there are too many scripts, you need to manually manage the loading sequence. The more scripts you have, the harder it is to manage.
  • Logical calls between different scripts need to be made in the way of global variables.
  • There is nohtmlHow to reference the JS file when? This example is Node.js.

Node.js has the CommonJS module specification, and Webpack is compatible with CommonJS so that we can use the CommonJS specification to write front-end code.

The CommonJS module specification was initiated by the JavaScript community and popularized on Node.js, which later influenced browser-side JavaScript.

require

Require is an API used by the CommonJS module specification to import files to be used. For example, introducing lib.js:

require('./lib');
Copy the code

Require returns an empty object by default; Create two new files with the following contents:

// lib.js
console.log('this is lib');

// index.js
console.log('start require')
var lib = require('./lib'); // Returns an empty object by default
console.log('end require', lib);
Copy the code

Run it: node index.js

It is also available through exports to mount properties: string, function, object data, etc.

Add some code to lib.js

console.log('this is lib')

exports.hello = "world"
exports.add = function (a, b) {
  return a + b;
}
exports.obj = { hello: "Node" }
Copy the code

It seems that under the CommonJS module specification, it has an empty object like exports by default.

Since require returns such an object, what about modifying and adding properties to it?

// index.js
// Since require returns an object, what about modifying and adding properties?
lib.hello = 'node';
lib.update = '1234';
Copy the code
// lib.js
setTimeout(function() {
  console.log(exports)},500)
Copy the code

As you can see, the printed content is changed after adding a 500ms to lib.js. So be aware of this copy issue when exporting through exports. Some of you may have seen the CommonJS module output a shallow copy of a value, ES6 module output a reference to a value. So what’s going on here?

Require can also return data via module.exports, and the data type is unlimited, such as returning a function:

// lib.js
console.log('this is lib')

exports.hello = "world"
exports.add = function (a, b) {
  return a + b;
}
exports.obj = { hello: "Node" }
// setTimeout(function() {
// console.log(exports)
// }, 500)
module.exports = function minus(a, b) {
  return a - b;
}
Copy the code

As you can see: lib returns the output minus function.

Module. exports takes precedence over exports when requiring a module. If module.exports is specified, the object specified by module. Exports object.

npm

NPM believes that everyone is familiar with it, so here is just a brief introduction.

NPM is a package management tool for Node.js, which comes with NPM when installing Node.js. Packages are node.js modules written by others. We often use packages developed by others and put on node.js servers.

NPM initialization: NPM init, always press Enter when initialization, after which a package.json file will be generated; Or run the command NPM init -y, which generates a default package.json file with the same properties as NPM init all the way to carriage return.

The package.json file contains the following contents:

{
  "name": "node"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": ""."license": "ISC"
}

Copy the code
  • Download and install dependency packages<packageName>:npm install <packageName>; Add if you want a global installation-g:npm install <packageName> -g. Such as the installationglobPackage:npm install glob
  • The command for uninstalling dependencies isnpm uninstall <packageName>.

For example, when you install the Express package, a Node-modules folder is generated, and the package you downloaded is stored in this folder:

If the speed of using NPM to install dependency packages is very slow, you can use Taobao mirror CNPM to install, mirror refers to it does a layer of foreign NPM package copy and map to the domestic server above, so that there is no mountain long water far away from foreign pull packages, the speed will be much faster.

Install CNPM:

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

CNPM is similar to NPM: CNPM install .

If you do not want to use CNPM for a long time, but some packages are really slow to download, you can use the image temporarily, such as installing Express:

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

–registry= indicates where to download dependencies. For example, some companies may have their own dependency server, so you can download dependencies faster by pointing this address to the company’s server address.

And CNPM itself is an alias of NPM, when using CNPM automatically to help us with the back of the parameters, registry=https://registry.npm.taobao.org, and then through mirror address to download package.

In addition, if you encounter problems with NPM, you can log on to the official website to find solutions:

conclusion

  • The node.js module specification is the CommonJS module specification.
  • CommonJS module specification approvedrequire()Load modules that return an object by default and can be setexportsmodule.exportsSets the data returned by the module.
  • Node.js’s package management tool is NPM, which can be used to improve download speed by using the image CNPM.