Programming Errors & Bugs
Open
Example from the ReviewMyCodes team
Node.js: "Cannot use import statement outside a module"
Description
I copied a script that uses import syntax. Running it with node gives a SyntaxError before anything runs.
— This is an example problem from the ReviewMyCodes team, showing how a good question looks. Real community questions appear alongside it.
Expected
The script should run.
Actual
SyntaxError: Cannot use import statement outside a module
Steps to reproduce
- Save the file as index.js
- Run node index.js
Code
javascript
import fs from "fs";
const config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
console.log(config.name);