What is the Node. Js
You can see this in the description on node.js website
Node.js® is a JavaScript runtime environment based on the Chrome V8 engine.
As a result, Node.js is not a standalone JS compiler, but a cross-platform JS runtime environment that runs on the server.
The Node modular
Node belongs to the CommonJS modularity specification. The custom ESM modularity specification was later than the CommonJS modularity implementation of Node, which resulted in a period of time when Node did not support ESM, and it is now able to support ESM modularity.
Node runs on the server, so it has built-in variables (such as the current file path), and new functions (such as file operations, path operations) must be operated through module references.
Variable provided by Node(CommonJS)
The Node runtime injected variables look something like this, as you can see in the documentation.
function module(__filename, __dirname, require.module.exports) {
// Write the code
module.export = { xxx: 'name' }
// Built-in return
return module.exports
}
Copy the code
__filename
: Absolute path of the current file__dirname
: Indicates the folder path of the current filerequire
: The method used to import other modules, with some methods and propertiesrequire.cache
: Caches loaded modulesrequire.resolve(request)
: Return passrequest
Address of the resolved filerequire.resolve.paths(request)
: Return parsingrequest
Is the search path
require.main
: startingNode
The entry script for the program
module
: some built-in methods and propertiesmodule.exports
: The object that the module finally exportsmodule.children
: dependent module (? Suspicious),module.filename
: Absolute path of the current filemodule.id
: Unique identifier of a modulemodule.filename
module.loaded
: indicates whether the module has been loaded or is being loadedmodule.parent
: Parent module for parsingmodule.paths
: Path for module searchmodule.require(id)
With:require
exports
:module.exports
The alias
Deprecated properties/methods are not covered here
Variables provided by Node(ESM)
No __filename, __dirname, require, module, exports, only import.
The current file path can be obtained from import.meta.url.
CommonJS and ESM differences in Node
Default to CommonJS modularity code in Node, so to enable ESM modularity you need to:
- in
package.json
Enable ‘type’ : ‘module’ in - use
.mjs
End of file
Mixed modularity is not recommended, so I won’t cover mixed usage here.
Let’s start with an example of CommonJS:
// counter.js
let count = 3
function addCounter() {
count++
}
module.exports = {
count,
addCounter,
}
// index.js
const { addCounter, count } = require('./counter')
addCounter()
console.log(count) / / 3
Copy the code
Regarding the ESM:
// counter.js
export let count = 3
export function addCounter() {
count++
}
// index.js
import { addCounter, count } from './counter.js'
addCounter()
console.log(count) / / 4
Copy the code
This is a classic example where you can see that the two results are different because Node handles them differently.
The subtotal
- Modular applications are different
CommonJS
Exposed modules are carried outrequire
Returns a copy of the entire valueESM
Exposed modules are carried outimport
A reference to the entire value is returned
- Different injection variables
CommonJS
Some variables are injectedESM
Will only injectimport
- Support for import is different
CommonJS
Support for importingJSON
File and omit other modules in the current folder.js
The suffix.ESM
Import not supportedJSON
And can’t be omitted.js
The suffix
There may be other differences, but I won’t go into them here.
conclusion
This article mainly introduces briefly what Node is and some knowledge of Node modularity. Build a Node basic knowledge framework.