Skip to content

"type": "module" causes SyntaxError: Cannot use import statement outside a module in node >= 20.6.0 #96

@adriencaccia

Description

@adriencaccia

Hey,

With node>=20.6.0, using "type": "module", running node --loader esbuild-register/loader -r esbuild-register foo.ts where foo.ts is

import { bar } from "./bar";

console.log("works");
console.log(bar);

Results in the following error:

/home/runner/work/esbuild-register-node20/esbuild-register-node20/foo.ts:1
import { bar } from "./bar";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at loadCJSModule (node:internal/modules/esm/translators:154:23)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:233:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:308:24)
    at async loadESM (node:internal/process/esm_loader:42:7)
    at async handleMainPromise (node:internal/modules/run_main:66:[12](https://github.com/adriencaccia/esbuild-register-node20/actions/runs/6177908642/job/16770040562#step:6:13))

Node.js v[20](https://github.com/adriencaccia/esbuild-register-node20/actions/runs/6177908642/job/16770040562#step:6:21).6.0

I made a minimal reproduction at https://github.com/adriencaccia/esbuild-register-node20.

You can see in this job that it works up to [email protected], but fails for 20.6.0 and 20.6.1.

If you have any idea how to navigate from here, with the changelog of node 20.6.0, I would be happy to help fix the issue!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions