ERR_REQUIRE_ESM with jiti/register
Environment
OS: Mac Sequoia 15.2 Node.js: 20.18.1 Package Manager: [email protected] jiti: 2.4.2 TypeScript: 5.7.2 Fastify: 5.2.0
Reproduction
https://github.com/yshrsmz/jiti-watch-test
Describe the bug
When running Fastify app with node --import jiti/register src/index.ts, it throws an ERR_REQUIRE_ESM error.
I couldn't reproduce the error when I directly used thread-stream(it might be because I didn't fully understand thread-stream), so the repro has a bit big dependency.
Additional context
No response
Logs
$ pnpm dev
> [email protected] dev /Users/yshrsmz/repos/github.com/yshrsmz/jiti-watch-test
> JITI_DEBUG=1 node --watch --import jiti/register src/index.ts
[jiti] [init] version: 2.4.2 module-cache: true fs-cache: true interop-defaults: true
[jiti] [cache] [hit] ./src/index.ts ~> ./node_modules/.cache/jiti/src-index.c030801c.mjs
[jiti] [init] version: 2.4.2 module-cache: true fs-cache: true interop-defaults: true
[jiti] [native] [import] ./node_modules/.pnpm/[email protected]/node_modules/fastify/fastify.js
[jiti] [cache] [hit] ./node_modules/.pnpm/[email protected]/node_modules/fastify/fastify.js ~> ./node_modules/.cache/jiti/fastify-fastify.26d881ec.mjs
[jiti] Native import error: {"code":"ERR_REQUIRE_ESM"}
[jiti] [fallback] ./node_modules/.pnpm/[email protected]/node_modules/fastify/fastify.js
[jiti] [cache] [hit] ./node_modules/.pnpm/[email protected]/node_modules/fastify/fastify.js ~> /var/folders/0w/sff60qpj5b39qk2mt2b6rdc43l5srr/T/jiti/fastify-fastify.26d881ec.mjs
[jiti] [transpile] [esm] ./node_modules/.pnpm/[email protected]/node_modules/fastify/fastify.js (0.497ms)
[jiti] [native] [require] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/schema-controller.js
[jiti] Native require error: {}
[jiti] [fallback] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/schema-controller.js
[jiti] [cache] [hit] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/schema-controller.js ~> /var/folders/0w/sff60qpj5b39qk2mt2b6rdc43l5srr/T/jiti/lib-schema-controller.cf868271.cjs
[jiti] [native] [require] ./node_modules/.pnpm/@[email protected]/node_modules/@fastify/fast-json-stringify-compiler/index.js
[jiti] [native] [require] ./node_modules/.pnpm/@[email protected]/node_modules/@fastify/ajv-compiler/index.js
[jiti] [native] [require] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/pluginUtils.js
[jiti] [native] [require] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/reqIdGenFactory.js
[jiti] [native] [require] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/route.js
[jiti] [native] [require] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/fourOhFour.js
[jiti] [native] [require] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/initialConfigValidation.js
[jiti] [native] [require] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/pluginOverride.js
[jiti] [native] [require] ./node_modules/.pnpm/[email protected]/node_modules/fastify/lib/noop-set.js
node:internal/modules/esm/loader:568
throw new ERR_REQUIRE_ESM(url, true);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module file:///Users/yshrsmz/repos/github.com/yshrsmz/jiti-watch-test/node_modules/.pnpm/[email protected]/node_modules/thread-stream/package.json not supported.
at Object.<anonymous> (/Users/yshrsmz/repos/github.com/yshrsmz/jiti-watch-test/node_modules/.pnpm/[email protected]/node_modules/thread-stream/index.js:3:21) {
code: 'ERR_REQUIRE_ESM'
}
Node.js v20.18.1
Failed running 'src/index.ts'
A simplified minimal reproduction: https://stackblitz.com/edit/github-svycnqv9?file=src%2Findex.js&startScript=dev
Although not the same error in StackBlitz, but it can be reproduced locally as described in this issue.
I have the same issue with an oclif CLI app. It uses ejs, which in turn imports the package version from its package.json file:
var _VERSION_STRING = require('../package.json').version;
which seems like a common pattern to me. This currently crashes JITI:
$ bin/dev.js
[jiti] [init] version: 2.4.2 module-cache: true fs-cache: true interop-defaults: true
[jiti] [cache] [hit] /node_modules/.pnpm/@[email protected]/node_modules/@oclif/core/lib/index.js ~> ./node_modules/.cache/jiti/lib-index+map.ac11944f.mjs
node:internal/modules/esm/translators:151
return cjsCache.get(job.url).exports;
^
TypeError: Cannot read properties of undefined (reading 'exports')
at require (node:internal/modules/esm/translators:151:33)
at Object.<anonymous> (/node_modules/.pnpm/[email protected]/node_modules/ejs/lib/ejs.js:54:23)
at loadCJSModule (node:internal/modules/esm/translators:165:3)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:207:7)
at ModuleJob.runSync (node:internal/modules/esm/module_job:261:35)
at require (node:internal/modules/esm/translators:150:9)
at Object.<anonymous> (/node_modules/.pnpm/@[email protected]/node_modules/@oclif/core/lib/config/config.js:40:26)
at loadCJSModule (node:internal/modules/esm/translators:165:3)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:207:7)
at ModuleJob.runSync (node:internal/modules/esm/module_job:261:35)