Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working for commonjs typescript that uses js extensions in local imports #770

Open
benasher44 opened this issue Apr 20, 2024 · 0 comments

Comments

@benasher44
Copy link

benasher44 commented Apr 20, 2024

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

@benasher44 benasher44 changed the title Not working for commonjs files that use js extensions in local imports Not working for commonjs typescript that uses js extensions in local imports Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant