Repro
Put these files next to each other in the same directory
// other_file.ts
export default function() {
console.log("hello");
}
// test.ts
import otherFile from "./other_file.js"
otherFile()
//.swcrc
{
"$schema": "https://json.schemastore.org/swcrc",
"module": {
"type": "commonjs"
},
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": true,
"dynamicImport": true
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
},
"target": "es2022",
"keepClassNames": true
}
}
Run: SWCRC=true node -r @swc-node/register test.ts
Expect
swc translates the .js import as if it's importing the .ts file, since it matches the .js ending in the final ts emit.
Actual
It crashes:
node:internal/modules/cjs/loader:1147
throw err;
^
Error: Cannot find module './other_file.js'
Require stack:
- /Users/basher/code/ashby/backend/test.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
at Function.Module._load (node:internal/modules/cjs/loader:985:27)
at Module.require (node:internal/modules/cjs/loader:1235:19)
at require (node:internal/modules/helpers:176:18)
at Object.<anonymous> (/Users/basher/code/ashby/backend/test.ts:5:60)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._compile (/Users/basher/code/ashby/backend/node_modules/pirates/lib/index.js:117:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Object.newLoader [as .ts] (/Users/basher/code/ashby/backend/node_modules/pirates/lib/index.js:121:7)
at Module.load (node:internal/modules/cjs/loader:1207:32) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/basher/code/ashby/backend/test.ts' ]
}
From this, it looks like swc isn't properly translating the import (to understand that it's importing the local ts file). I understand that this project is very ESM-focused these days, but supporting this would help projects migrate to ESM, since a big first step is switching to .js extensions + node supports .js endings in CommonJS modules.
I was using node 20.11.1, @swc/core 1.4.16, and @swc-node/register 1.9.0
Repro
Put these files next to each other in the same directory
// other_file.ts
// test.ts
//.swcrc
{ "$schema": "https://json.schemastore.org/swcrc", "module": { "type": "commonjs" }, "jsc": { "parser": { "syntax": "typescript", "tsx": true, "decorators": true, "dynamicImport": true }, "transform": { "legacyDecorator": true, "decoratorMetadata": true }, "target": "es2022", "keepClassNames": true } }Run:
SWCRC=true node -r @swc-node/register test.tsExpect
swc translates the .js import as if it's importing the .ts file, since it matches the .js ending in the final ts emit.
Actual
It crashes:
From this, it looks like swc isn't properly translating the import (to understand that it's importing the local ts file). I understand that this project is very ESM-focused these days, but supporting this would help projects migrate to ESM, since a big first step is switching to .js extensions + node supports .js endings in CommonJS modules.
I was using node 20.11.1, @swc/core 1.4.16, and @swc-node/register 1.9.0