swc-project / swc-node

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

Node.js v20.6+ no longer supports the loader API in favour of the register API #743

Closed doichev-kostia closed 5 months ago

doichev-kostia commented 7 months ago

According to the node.js release notes for node 20.6 the experimental loaders API was deprecated in favour of the register API. Because of that, we need to use node --import register.js file.ts instead of node --loader @swc-node/register/esm file.ts.

Currently, it's not possible or not mentioned in the docs how to solve this issue with @swc-node/register and the error the users may have is

node --loader @swc-node/register/esm ./src/main.ts
(node:68547) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("%40swc-node/register/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)

node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^
Error: Bindings not found.

Reproduction and workaround

I've added a repo with the reproduction and possible workaround influenced by tsx.

valeneiko commented 7 months ago

You can create a file to register @swc-node hooks:

import {register} from 'node:module';
import { pathToFileURL } from "node:url";

register('@swc-node/register/esm', pathToFileURL('./'));

And use it in the --import flag:

node --import ./register.js ./src/main.ts
barthuijgen commented 7 months ago

Not wanting to create an extra entrypoint file I've found the inline option to work fine as well.

{
  "scripts": {
    "dev": "node --import 'data:text/javascript,import {register} from \"node:module\";import {pathToFileURL} from \"node:url\";register(\"%40swc-node/register/esm\",pathToFileURL(\"./\"));' src/main.ts"
  }
}

This does make it quite verbose I hope a solution will emerge that lets us do this with a shorter syntax.

yeliex commented 5 months ago

I have created a pr to support this #748

before merged, try use https://npmjs.com/swc-register-esm temporary

ehaynes99 commented 5 months ago

Note: looks like they backported it, because node 18 also emits this warning.