swc-node icon indicating copy to clipboard operation
swc-node copied to clipboard

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

Open Phryxia opened this issue 2 years ago • 10 comments

Reproduction Procedure

  1. Clone this
  2. yarn
  3. yarn dev
  • Version
    • node: 14.18.3
    • @swc-node/register: 1.6.0 ~ 1.6.4
  • Executed Prompt: SWCRC=true node --loader @swc-node/register/esm index.ts
  • Note that < 1.6, this behavior fall back into #704

.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 ?

Phryxia avatar Apr 19 '23 00:04 Phryxia

same here

huseeiin avatar Jun 20 '23 08:06 huseeiin

same here, any news ?

sebaplaza avatar Jul 11 '23 13:07 sebaplaza

same issue. mac ok, liunx error

3lang3 avatar Sep 20 '23 03:09 3lang3

@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.

fatso83 avatar Sep 20 '23 07:09 fatso83

this issue occurs on node v20.8.0

iambumblehead avatar Oct 07 '23 22:10 iambumblehead

@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..

philSixZero avatar Oct 10 '23 15:10 philSixZero

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

psychobolt avatar Feb 09 '24 22:02 psychobolt

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

nnnnoel avatar Feb 19 '24 01:02 nnnnoel

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?

c3cris avatar Feb 23 '24 07:02 c3cris

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.

guptaashwanee avatar May 08 '24 06:05 guptaashwanee