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:
yarn init -y
The command will create a package.json
file inside the root folder.
{
"name": "node-demo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT"
}
Add a type
and scripts
to package.json
file:
{
"name": "node-demo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"type": "module",
"scripts": {
"start": "node index.js"
}
}
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 yarn start
. Here is the output on my computer:
yarn run v1.22.19
$ node index.js
Total memory 34359738368
Free memory 74137600
✨ Done in 0.33s.