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

Jiti handles imports incorrectly in Node 22.12+ after a require(esm) call fails #346

Closed
thecrypticace opened this issue Dec 17, 2024 · 4 comments · Fixed by #348
Closed
Labels
bug Something isn't working

Comments

@thecrypticace
Copy link
Contributor

Environment

OS: macOS 15.2
Node: v22.12.0
NPM: v10.9.0

Reproduction

https://github.com/thecrypticace/node-22-jiti-bug

Describe the bug

Node 22.12 backported require(esm) support and it appears there's a bug either in Node or in Jiti that's causing imported files to be handled incorrectly.

This bug reproduces under the following conditions:

  • A require(esm) call is used
  • That call fails and is caught by a try/catch block
  • Jiti is then used to import that same file

The imports inside that file may not return correct results. In this case a function is being returned as an empty object.

When Jiti is used by itself the bug does not happen — only in conjunction with a failed require(esm) call. The one in this repo fails because its importing a file without using a file extension:

import file from "./file";

If you add the file extension, the bug does not happen because the require(esm) call works:

import file from "./file.js";

Additional context

I'm one of the maintainer's for Tailwind CSS and several users have reported plugins not working in Tailwind CSS v3.x with the latest versions of Node.js. I tracked this down to a function being turned into an object — likely because of an issue with import handling.

This bug appears to affect:

  • Node v22.12+
  • Node v23.0+
  • macOS / Windows (haven't tested Linux but probably the same)
  • Jiti 2's sync and async methods for importing files are both affected
  • Jiti 1 is also affected (this is what we use in Tailwind CSS v3 currently)

Logs

N/A
@pi0
Copy link
Member

pi0 commented Dec 17, 2024

Thanks for the report and reproduction. I can confirm behavior change.

Will investigate to find a workaround to this (my guess is require-cache).

BTW are you aware that jiti itself always tries native import before fallback? (this means by upgrading to jiti v2 you can remove try/catch altogether)

@pi0
Copy link
Member

pi0 commented Dec 17, 2024

Update: I also can confirm the issue happens because Node.js require-cache will become half broken in the first require() attempt.

Current workaround is using requireCache: false option (or JITI_REQUIRE_CACHE=0) this makes jiti use its standalone cache.

@pi0
Copy link
Member

pi0 commented Dec 17, 2024

I prepared a solution however not sure if it is safe or better to ask Node.js fix it upstream.. #348

@pi0
Copy link
Member

pi0 commented Dec 17, 2024

v1.21.7 + v2.4.2 released with hotfix. (confirmed fix on your reproduction)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants