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

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

Closed
doichev-kostia opened this issue Nov 15, 2023 · 4 comments · Fixed by #748
Closed

Comments

@doichev-kostia
Copy link

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.

@doichev-kostia doichev-kostia changed the title Node > 20.6 is no longer support --loader argument Node > 20.6 is no longer supporting the loader API Nov 15, 2023
@doichev-kostia doichev-kostia changed the title Node > 20.6 is no longer supporting the loader API Node.js v20.6+ no longer supports the loader API in favour of the register API Nov 15, 2023
@valeneiko
Copy link

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
Copy link

barthuijgen commented Dec 6, 2023

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
Copy link
Contributor

yeliex commented Jan 12, 2024

I have created a pr to support this #748

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

@ehaynes99
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

5 participants