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 file
  • require: The method used to import other modules, with some methods and properties
    • require.cache: Caches loaded modules
    • require.resolve(request): Return passrequestAddress of the resolved file
      • require.resolve.paths(request): Return parsingrequestIs the search path
    • require.main: startingNodeThe entry script for the program
  • module: some built-in methods and properties
    • module.exports: The object that the module finally exports
    • module.children: dependent module (? Suspicious),
    • module.filename: Absolute path of the current file
    • module.id: Unique identifier of a modulemodule.filename
    • module.loaded: indicates whether the module has been loaded or is being loaded
    • module.parent: Parent module for parsing
    • module.paths: Path for module search
    • module.require(id)With:require
  • exports: module.exportsThe 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:

  • inpackage.jsonEnable ‘type’ : ‘module’ in
  • use.mjsEnd 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

  1. Modular applications are different
    • CommonJSExposed modules are carried outrequireReturns a copy of the entire value
    • ESMExposed modules are carried outimportA reference to the entire value is returned
  2. Different injection variables
    • CommonJSSome variables are injected
    • ESMWill only injectimport
  3. Support for import is different
    • CommonJSSupport for importingJSONFile and omit other modules in the current folder.jsThe suffix.
    • ESMImport not supportedJSONAnd can’t be omitted.jsThe 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.