ECMAScript 2015 (ES6)
You can also use the ES6 module system with Node. Add a package.json
to the root of your Node project. You can do this by running the following command in the terminal:
pnpm init
The command will create a package.json
file inside the root folder.
{
"name": "node-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Add a type
and update the scripts
:
{
"name": "node-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Now we can use ES6 syntax! Update the index.js
as follows:
import os from "os";
console.log(`Total memory ${os.totalmem()}`);
console.log(`Free memory ${os.freemem()}`);
The run it with pnpm start
. Here is the output on my computer:
> node index.js
Total memory 34359738368
Free memory 100777984