CommonJS specification is proposed to make up for the lack of standard JavaScript, in order to achieve the basic ability to develop large applications.
Nodejs, nodeJS, nodeJS, nodeJS, nodeJS, nodeJS, nodeJS, nodeJS, nodeJS, nodeJS, nodeJS In fact, I heard this a lot at school, and even got into a fight with my roommate about it (not to mention that). Now JS is not limited to the original era, now JS is responsible for more and more, we urgently need a standard, to make JS full play.
What problems have been solved by CommonJS
Pu Ling teacher in the book is also mentioned several JS standards on the defects.
1. No modular system
I don’t know if you’ve worked with Java or Python before, but it’s very easy to reference and export whatever you need from file to file. Then sadly looking back at our JS, there was no concept of module in the past, JS just did form verification and didn’t feel anything, when our project became bigger and bigger, the project became more and more difficult to maintain, at this time we urgently needed a module mechanism.
2. There are few standard libraries
ECMAScript defines very few core libraries. There are no standard libraries like Java for file systems, networks, and I/O streams, which are common requirements when developing large applications, but not in JS.
3. There is no standard interface
We have not seen in JS such as database, web server and so on standard unified interface, this is also very inconvenient.
4. Lack of package management system
Well, I’ve been trying to make fun of this for a long time. When I asked my classmates a question and they all said, “Go guide a bag,” I wanted to tell them something. Without a package management system, we would not be able to easily auto-load, install dependencies, and use packages written by others.
CommonJS provides a roadmap for JS development, covering modules, binaries, I/O streams, package management, process environments, and more. Node uses CommonJS ‘Modules specification to implement a very easy module system, and NPM’s implementation of the package management specification is another great innovation that makes development a lot easier.
CommonJS module specification
1. Module definition
An exports object is provided in a module that exports variables or methods of the current module. There is also a module object in the module, which represents the current module itself. Exports is an attribute of module. In Node, a file is a module.
// a.js
exports.a = 1;Copy the code
2. Module identifier
The module identifier is the argument passed to the require method, which must be a string with a small camel name, or an absolute relative path.
3. Module introduction
Importing a module is also very simple. There is a require() method in a module that accepts the module identifier and imports a module.
// b.js
const { a } = require('./a'); / / 1Copy the code
Module in Node
Let’s start with a piece of code.
And then we run it, and we look in the browser.
We see this in the browser, and it’s kind of weird, isn’t it, that we didn’t write so much code. It turns out Nodejs did this for us quietly, adding the code at the beginning and end of our code. Let’s see, what are the arguments that are passed in to this function? Exports and module.exports require that the __filename and __ dirname name of this module be used as file and folder paths.
Exports and the module exports
You can see that when we finished executing the code, there was a log method mounted on both exports and module.exports, which we exported. Exports is a reference to module. Exports, which is the only export of a module. Exports is a reference to module. So it reassigns it, and it doesn’t export anymore.
4. require
There are actually a couple of things to say about the require rule.
- / represents an absolute path, and./ represents a relative path
- The default extension is js, but if you don’t write it you’ll try.js,.json,.node.
- If you didn’t write a path when you introduced it, you would go to a built-in module, such as HTTP, and then go back to node_modules, one level up, until you get to the root path.
- Node also has a cache for modules, you load the first time, the second time will be found in the cache, cache is higher than the built-in module, is the highest priority.
I feel that the content is a little bit too much. Today, I have read a lot of grass. I hope you can forgive me and give me valuable suggestions.