Skip to content

@swc-node/register can not handle es modules #634

Closed
@cschroeter

Description

@cschroeter
yarn run v1.22.17
$ node -r @swc-node/register index.ts
/home/christian/Workspaces/swc-modules/index.ts:141
undefined
      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/christian/Workspaces/swc-modules/node_modules/pkg-up/index.js from /home/christian/Workspaces/swc-modules/index.ts not supported.
Instead change the require of index.js in /home/christian/Workspaces/swc-modules/index.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.newLoader [as .js] (/home/christian/Workspaces/swc-modules/node_modules/pirates/lib/index.js:141:7)
    at Object.<anonymous> (/home/christian/Workspaces/swc-modules/index.ts:2:14)
    at Module._compile (/home/christian/Workspaces/swc-modules/node_modules/pirates/lib/index.js:136:24)
    at Object.newLoader [as .ts] (/home/christian/Workspaces/swc-modules/node_modules/pirates/lib/index.js:141:7) {
  code: 'ERR_REQUIRE_ESM'
}

Here is an minimal demo of that isssue https://github.com/cschroeter/bug-swc-esmodules/blob/main/package.json

Here is how ts-node tackles the issue TypeStrong/ts-node#1007

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions