Run js file with Node.js
node command followed by the file name. For example, if you have a file named
script.js, you can run it using the following command:
You can also pass arguments to your script by appending them to the
node command after the file name. For example, if your script takes in two arguments, you can run it like this:
node script.js arg1 arg2
node command. You can check if you have Node.js installed by running the following command:
If you don't have Node.js installed, you can download and install it from the official Node.js website.
Node: Run js file in experimental mode
--experimental-modules flag when running the
node command. This flag enables support for the ECMAScript Modules (ESM) proposal, which allows you to use the
export statements in your code.
For example, if you have a file named
script.mjs that uses the
import statement, you can run it using the following command:
node --experimental-modules script.mjs
Remember that the
--experimental-modules flag is only available in Node.js version 13.2.0 or higher. You can check your Node.js version by running the
node -v command.
.mjs file extension for Node.js to treat it as an ECMAScript module. This is because the
export statements are only allowed in files with the
.mjs extension when the
--experimental-modules flag is used.
For more information on ECMAScript modules and how to use them in Node.js, you can refer to the official Node.js documentation.
What is Node.js
Node.js also has a large and active community, contributing many modules and packages that can be easily installed and used in Node.js applications. This makes it easy for developers to extend their applications' functionality and leverage others' work in the community.