swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.72k stars 71 forks source link

@swc-node/core@1.10.0 - Cannot use import statement outside a module #693

Closed jasongerbes closed 1 year ago

jasongerbes commented 1 year ago

The latest version of @swc-node/core (version 1.10.0) has introduced the following issue:

Cannot use import statement outside a module
/Users/jasongerbes/Git/vista-digital-2/libs/nx-plugin/src/executors/generate-certificate/executor.ts:1
import _object_spread from "@swc/helpers/src/_object_spread.mjs";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1084:15)
    at Module._compile (node:internal/modules/cjs/loader:1119:27)
    at Module._compile (/Users/jasongerbes/Git/vista-digital-2/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
    at Object.newLoader [as .ts] (/Users/jasongerbes/Git/vista-digital-2/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1033:32)
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Module.require (node:internal/modules/cjs/loader:1057:19)
    at require (node:internal/modules/cjs/helpers:103:18)

This issue appeared after upgrading to:

And didn't occur with the versions used before the upgrade:

This issue may be related to #648 and may have been introduced by #643.

thgh commented 1 year ago

The issue is specifically introduced in @swc-node/core@1.10.0 (the version of @swc-node/register doesn't matter).

Here is a workaround: (yarn)

  "resolutions": {
    "@swc-node/core": "1.9.2"
  }
jasongerbes commented 1 year ago

@thgh it's worth noting that @swc-node/register@1.6.1 depends on @swc-node/core@1.10.0.

For compatibility, it may be safest to downgrade to an older version of @swc-node/register as a workaround for this issue.

jasongerbes commented 1 year ago

Thanks @Brooooooklyn! I can confirm that #694 fixed the issue

cnwangjie commented 2 months ago

Still encountering this problem on @swc-node/core@1.13.1.

@Brooooooklyn, Can you kindly confirm whether the subsequent (after 1.10.4) changes will break something?