Prerequisites
Fastify version
4.10.0
Plugin version
No response
Node.js version
20.1.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
13.3.1
Description
I think this is a Node issue, but only @fastify/autoloader is affected by it, so I am opening an issue with you good folks as well in case this is really a Fastify issue instead or if it is a Node issue but you are able to find a workaround!
fastify.register(fastifyAutoload, {
dir: join(__dirname, 'routes'),
forceESM: true,
});
This piece of code works in Node 19 when using node --loader=ts-node/esm (the route files are in TypeScript) but not on Node 20.
I have come up with a repro here: https://github.com/TomasHubelbauer/node-esm-loader-repro
Steps to Reproduce
Follow my repro repo: https://github.com/TomasHubelbauer/node-esm-loader-repro
Steps copied here for posterity.
Node 20:
nvm install 20 to install Node 20
node --version to ensure Node version (I get 20.1.0)
npm install to install dependencies
npm run test to run the health.test.ts script
Notice the test fails and Fastify's autoload is seemingly not aware of the
--loader option and attempts to load routes/health.ts without TypeScript to
JavaScript conversion via ts-node/esm.
npm run test
> repro@0.0.0 test
> node --loader=ts-node/esm --experimental-specifier-resolution=node --test health.test.ts
ℹ (node:95105) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
ℹ (Use `node --trace-warnings ...` to show where the warning was created)
✖ should be alive (32.750836ms)
Error: "@fastify/autoload cannot import plugin at '/routes/health.ts'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app."
at findPlugins (/node_modules/@fastify/autoload/index.js:224:15)
at async autoload (/node-esm-loader-repro/node_modules/@fastify/autoload/index.js:35:22)
ℹ tests 1
ℹ suites 0
ℹ pass 0
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 2208.335483
✖ failing tests:
✖ should be alive (32.750836ms)
Error: "@fastify/autoload cannot import plugin at 'routes/health.ts'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app."
at findPlugins (/node_modules/@fastify/autoload/index.js:224:15)
at async autoload (/node_modules/@fastify/autoload/index.js:35:22)
Node 19:
nvm install 19 to install Node 20
node --version to ensure Node version (I get 19.9.0)
npm install to install dependencies
npm run test to run the health.test.ts script
Notice the test passes and Fastify's autoload is inherit the --loader option
and uses the ts-node/esm loader successfully to auto-load routes/health.ts.
npm run test
> repro@0.0.0 test
> node --loader=ts-node/esm --experimental-specifier-resolution=node --test health.test.ts
ℹ (node:95453) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
ℹ (Use `node --trace-warnings ...` to show where the warning was created)
✔ should be alive (472.711395ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 2679.742161
Expected Behavior
Works the same way in both 19 and 20.
Prerequisites
Fastify version
4.10.0
Plugin version
No response
Node.js version
20.1.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
13.3.1
Description
I think this is a Node issue, but only
@fastify/autoloaderis affected by it, so I am opening an issue with you good folks as well in case this is really a Fastify issue instead or if it is a Node issue but you are able to find a workaround!This piece of code works in Node 19 when using
node --loader=ts-node/esm(the route files are in TypeScript) but not on Node 20.I have come up with a repro here: https://github.com/TomasHubelbauer/node-esm-loader-repro
Steps to Reproduce
Follow my repro repo: https://github.com/TomasHubelbauer/node-esm-loader-repro
Steps copied here for posterity.
Node 20:
nvm install 20to install Node 20node --versionto ensure Node version (I get 20.1.0)npm installto install dependenciesnpm run testto run thehealth.test.tsscriptNotice the test fails and Fastify's
autoloadis seemingly not aware of the--loaderoption and attempts to loadroutes/health.tswithout TypeScript toJavaScript conversion via
ts-node/esm.Node 19:
nvm install 19to install Node 20node --versionto ensure Node version (I get 19.9.0)npm installto install dependenciesnpm run testto run thehealth.test.tsscriptNotice the test passes and Fastify's
autoloadis inherit the--loaderoptionand uses the
ts-node/esmloader successfully to auto-loadroutes/health.ts.Expected Behavior
Works the same way in both 19 and 20.