[Node.js] Set the Node.js Project to ECMAScript Modules (ESM)
Node.js projects have two ways that you can import and export code into different files. This is through CommonJS (CJS) and ECMAScript modules (ESM).
CommonJS is Node's default way to import modules but because I like to have my Node.js code similar to my frontend code, we will use ESM for this project.
To do so we will set a project type
in our package.json
.
"type":"module",
So, CommonJS module:
const http = require('http');
module.exports = {
name: 'JS'
}
ESM Module:
import http from "http";
export const name = "JS"