swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.69k stars 69 forks source link

@swc-node/register/esm doesn't recognize `.ts` file #710

Open Phryxia opened 1 year ago

Phryxia commented 1 year ago

Reproduction Procedure

  1. Clone this
  2. yarn
  3. yarn dev

.swcrc

{
  "jsc": {
    "baseUrl": ".",
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "dynamicImport": true
    },
    "target": "es5"
  },
  "module": {
    "type": "es6"
  }
}

Symptoms

yarn run v1.22.19
$ SWCRC=true node --loader @swc-node/register/esm index.ts
(node:19817) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

[Error: ENOENT: no such file or directory, open '/home/sekyu-kwon/temp/index.ts.mjs'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/home/my-name/temp/index.ts.mjs'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

As you can see, it doesn't recognize .ts. Also same with .mts.

Is it related to .swcrc recognition issues like #701 ?

huseeiin commented 1 year ago

same here

sebaplaza commented 12 months ago

same here, any news ?

3lang3 commented 9 months ago

same issue. mac ok, liunx error

fatso83 commented 9 months ago

@3lang3 Do you use the same Node versions on both platforms? I see the original issue is using Node 14 which is not a supported LTS release anymore.

iambumblehead commented 9 months ago

this issue occurs on node v20.8.0

philSixZero commented 9 months ago

@Brooooooklyn After looking into the sources, I found that (at least under windows) the following if clause is causing the issue: https://github.com/swc-project/swc-node/blob/cc3d1da4f732e21c64e749766328705d4f6cfad5/packages/register/register.ts#L69-L73 The compile method of the loader is not transpiling the source file at all, since the filename and the included files provided by the tsconfig options are never equal.

Removing the if-clause makes it work as expected. But after doing some benchmarks with the ESM loaders from @swc-node and @esbuild-kit, @esbuild-kit was almost twice as fast as @swc-node. So I'm definitely sticking with @esbuild-kit..

psychobolt commented 5 months ago

I can confirm that @philSixZero finding is the same issue for Windows. In my case when there are multiple workspaces in a project. I have a subproject which requires to load their own tsconfig.json. I ended up using dotenv-cli to set the project variable for a Yarn workspace path e.g. TS_NODE_PROJECT=$INIT_CWD/tsconfig.json, however only in linux it works since the url needs this fix as well https://github.com/swc-project/swc-node/pull/754

nnnnoel commented 4 months ago

@Brooooooklyn @Phryxia I think that issue has been resolved via this commit

c3cris commented 4 months ago

I can confirm that it is a windows issue. I have tested this on Linux, Mac and Windows. If tsconfig.json exists in subfolder of a workspace it throws an error. If not tsconfig.json exists it runs fine even on Windows.

node --no-warnings=ExperimentalWarning --loader @swc-node/register/esm src/server.ts

"@swc-node/register": "^1.8.0"
node 20.11.1 

@nnnnoel Does that fix this as well?

guptaashwanee commented 2 months ago

This is my dev script to run the dev server

"dev": "node --watch --no-warnings=ExperimentalWarning --loader @swc-node/register/esm src/index.ts",

It works perfectly fine.