swc-project / swc-node

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

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

Closed danieltroger closed 7 months ago

danieltroger commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 7 months ago

swc-node now works with node 21.2.0 without a problem, after updating to yarn 4.0.2 (thanks to https://github.com/yarnpkg/berry/pull/5961)