Skip to main content
The usual way to run a Node.js program is to run the globally available node command (once you install Node.js) and pass the name of the file you want to execute. If your main Node.js application file is app.js, you can call it by typing:
node app.js

Using a shebang line

You can embed interpreter information directly into your JavaScript file with a shebang line. The shebang is the first line in the file, and tells the OS which interpreter to use for running the script.
#!/usr/bin/node
The example above gives the absolute path of the interpreter. Not all operating systems have node in the bin folder, but all should have env. You can tell the OS to run env with node as a parameter:
#!/usr/bin/env node

// your javascript code
To use a shebang, your file must have executable permission. Grant app.js executable permission by running:
chmod u+x app.js
When running the command, make sure you are in the same directory that contains the app.js file.

Pass a string as argument to node instead of a file path

To execute a string as an argument, use -e / --eval "script". This evaluates the argument as JavaScript. Modules that are predefined in the REPL can also be used in the script.
node -e "console.log(123)"
On Windows, using cmd.exe a single quote will not work correctly because it only recognizes double " for quoting. In PowerShell or Git Bash, both ' and " are usable.

Restart the application automatically

As of Node.js v16, there is a built-in option to automatically restart the application when a file changes. This is useful for development purposes. Pass the --watch flag to Node.js:
node --watch app.js
When you change the file, the application will restart automatically. Read the --watch flag documentation for more details.

Run a task with Node.js

Node.js provides a built-in task runner that allows you to execute specific commands defined in your package.json file. This is useful for automating repetitive tasks such as running tests, building your project, or linting your code.

Using the --run flag

The --run flag allows you to run a specified command from the scripts section of your package.json. For example, given the following package.json:
{
  "type": "module",
  "scripts": {
    "start": "node app.js",
    "dev": "node --run start -- --watch",
    "test": "node --test"
  }
}
You can run the test script using the --run flag:
node --run test

Passing arguments to the command

The dev key in the scripts object above uses the -- --another-argument syntax to pass arguments to the command. In this case, --watch is passed to the dev script:
node --run dev

Environment variables

The --run flag sets specific environment variables that can be useful for your scripts:
  • NODE_RUN_SCRIPT_NAME: The name of the script being run.
  • NODE_RUN_PACKAGE_JSON_PATH: The path to the package.json file being processed.

Intentional limitations

The Node.js task runner is intentionally more limited compared to other task runners like npm run or yarn run. It focuses on performance and simplicity, omitting features like running pre or post scripts. This makes it suitable for straightforward tasks but may not cover all use cases.

Build docs developers (and LLMs) love