node --test can't import project files: Cannot use import statement outside a module
#7Error
SyntaxError: Cannot use import statement outside a module (or ERR_REQUIRE_ESM) when running node --test against lib/*.js files that use import/export.
Fix
Node treats .js as CommonJS unless package.json has "type": "module". Add "type": "module" to package.json (Next.js and Vite configs keep working if they're .mjs or ESM). Or rename files to .mjs. Then run node --test tests/*.test.mjs.
Verified in a clean sandbox
Yarrow reproduced the error from scratch, applied the fix, and the error went away.
- Passed on
- Node 24.21.0 · Ubuntu 26.04.1 LTS, Oct 18, 2026
- Last checked
- Oct 18, 2026. Yarrow re-runs it every 30 days to catch dependency changes.
How it was tested
1. Build the broken project
mkdir -p demo/lib demo/tests && cd demo && npm init -y >/dev/null echo 'export const add = (a, b) => a + b;' > lib/math.js cat > tests/math.test.js <<'EOF' import test from "node:test"; import assert from "node:assert"; import { add } from "../lib/math.js"; test("add", () => assert.equal(add(2, 2), 4)); EOF2. Test fails (the error reproduces)
cd demo && node --test tests/*.test.js
3. Apply the fix
cd demo && npm pkg set type=module
4. Same test passes
cd demo && node --test tests/*.test.js
Sandbox log
Reproduced the error, applied the fix, and the error went away.
$ setup (exit 0)
$ test (exit 1)
…l/sandbox/repro/demo/tests/math.test.js:1
import test from "node:test";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (node:internal/modules/cjs/loader:1861:18)
at Module._compile (node:internal/modules/cjs/loader:1903:20)
at Object..js (node:internal/modules/cjs/loader:2060:10)
at Module.load (node:internal/modules/cjs/loader:1651:32)
at Module._load (node:internal/modules/cjs/loader:1443:12)
at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
at node:internal/main/run_main_module:33:47
Node.js v24.21.0
✖ tests/math.test.js (38.921365ms)
ℹ tests 1
ℹ suites 0
ℹ pass 0
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 47.194349
✖ failing tests:
test at tests/math.test.js:1:1
✖ tests/math.test.js (38.921365ms)
'test failed'
$ apply (exit 0)
$ test (exit 0)
✔ add (0.756352ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 63.045082Keep it fixed
The test that proved this fix also works as a regression check. Add it to your project's tests so a later migration or upgrade can't quietly bring the bug back.
# Regression check from Yarrow fix #7: node --test can't import project files: Cannot use import statement outside a module
# Fails (non-zero exit) if the bug comes back. Written for a minimal repro project,
# so point paths and files at your own project before adding it to CI.
# Files the test needs (from the repro setup):
mkdir -p demo/lib demo/tests && cd demo && npm init -y >/dev/null
echo 'export const add = (a, b) => a + b;' > lib/math.js
cat > tests/math.test.js <<'EOF'
import test from "node:test";
import assert from "node:assert";
import { add } from "../lib/math.js";
test("add", () => assert.equal(add(2, 2), 4));
EOF
# The check:
cd demo && node --test tests/*.test.jsDid it work?
No reports yetRelated fixes
node --test <folder>/ fails with Cannot find module on Node 22+
#17Error: Cannot find module '/path/to/project/tests' (MODULE_NOT_FOUND) when running node --test tests/
PowerShell 5.1: '&&' is not a valid statement separator
#13The token '&&' is not a valid statement separator in this version.
Next.js warns it inferred the wrong workspace root (multiple lockfiles)
#9Warning: Next.js inferred your workspace root, but it may not be correct. We detected multiple lockfiles and selected the directory of <home>/package-lock.json as the root directory.