Run a JavaScript file with Node.js

node-js

Updated August 29, 2026

Save a JavaScript file, open a terminal in its directory, and pass the filename to Node:

node app.js

The path can be relative or absolute. For example:

node ./scripts/check.mjs

Node treats .mjs as an ES module. A .js file is an ES module when the nearest package.json contains "type": "module"; otherwise Node normally treats it as CommonJS. Use matching syntax rather than changing extensions at random.

For a project, define a script in package.json:

{
  "scripts": { "start": "node src/index.js" }
}

Then run npm start. If Node reports that it cannot find a module or file, check the working directory with pwd and the exact path. If it reports a syntax or runtime error, the file was found—the next step is to read that error rather than reinstall Node. node --check app.js can catch some syntax errors without executing the file.

Sources

related.

Ruslan Osipov

Ruslan Osipov

About the author