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

swc-node doesn't work with latest node version #736

Closed
danieltroger opened this issue Oct 26, 2023 · 3 comments
Closed

swc-node doesn't work with latest node version #736

danieltroger opened this issue Oct 26, 2023 · 3 comments

Comments

@danieltroger
Copy link

danieltroger commented Oct 26, 2023

It seems like swc-node broke after node 20.5.1?

To reproduce, execute this:

nvm install 20.9.0
nvm use 20.9.0
mkdir swc-node-test
cd swc-node-test
echo "{}" > package.json
touch yarn.lock
yarn set version berry
echo 'console.log("Hello world")' > index.ts
yarn add @swc-node/register @swc/core typescript
yarn node --loader @swc-node/register/esm index.ts

You should see the following output:

node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^
TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected array buffer, or typed array to be returned for the "source" from the "transformSource" function but got undefined.
    at new NodeError (node:internal/errors:406:5)
    at assertBufferSource (node:internal/modules/esm/translators:84:9)
    at stringify (node:internal/modules/esm/translators:94:3)
    at createCJSModuleWrap (node:internal/modules/esm/translators:219:12)
    at ModuleLoader.commonjsStrategy (node:internal/modules/esm/translators:297:10) {
  code: 'ERR_INVALID_RETURN_PROPERTY_VALUE'
}

Node.js v20.9.0

Edit: same thing happens with node v21.1.0

@danieltroger
Copy link
Author

I looked into this a bit. I have no idea how this whole node module resolving works, but the error thrown originates from a worker with the url "node:internal/modules/esm/worker", but I don't know how to get a debugger into the worker

Screenshot 2023-11-07 at 13 55 28

@danieltroger
Copy link
Author

danieltroger commented Nov 7, 2023

Ok I looked into this a bit more and it seems that what happens is:

  1. readFileSync is imported from fs here https://github.com/nodejs/node/blob/main/lib/internal/modules/esm/translators.js#L35 but for some reason this is before yarn pnp has patched the fs module
  2. Reading the file contents here fails but node chooses to proceed anyway
  3. Node runs into some error in an assert

See in this screenshot where pnp isn't in the stack trace when calling the readFileSync function available in the scope and it fails, while require("fs").readFileSync succeeds
Screenshot 2023-11-07 at 22 50 32

For comparison, here's the correctly patched fs module's stack trace
Screenshot 2023-11-07 at 22 51 30

In conclusion this seems to be an issue with yarn.

Edit: can confirm it's a yarn bug, swc-node works fine with npm

@danieltroger
Copy link
Author

swc-node now works with node 21.2.0 without a problem, after updating to yarn 4.0.2 (thanks to yarnpkg/berry#5961)

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