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

Jest with Node.js v20.11.0 does not understand overlayscrollbars-react #604

Closed
krutoo opened this issue Jan 23, 2024 · 17 comments
Closed

Jest with Node.js v20.11.0 does not understand overlayscrollbars-react #604

krutoo opened this issue Jan 23, 2024 · 17 comments

Comments

@krutoo
Copy link

krutoo commented Jan 23, 2024

Hi, i noticed about "module" field in package.json is not standard way to define ESM entry point of package.

According this answer:
jestjs/jest#9430 (comment)

Can we update package.json of overlayscrollbars packages for to comply with the standard?

@KingSora
Copy link
Owner

KingSora commented Jan 23, 2024

Good day @krutoo

overlayscrollbars-react package.json looks like this: https://cdn.jsdelivr.net/npm/overlayscrollbars-react@0.5.3/package.json

It defines the exports, main and module fields.

@krutoo
Copy link
Author

krutoo commented Jan 23, 2024

@KingSora hmmm, for some reason Jest does not understand that this file must be interpreted as an ESM

@KingSora
Copy link
Owner

KingSora commented Jan 23, 2024

@krutoo Would you be able to create an example repository with your jest setup and a library which works with it? I could take a look what could potentially be wrong there.

@krutoo
Copy link
Author

krutoo commented Jan 23, 2024

@KingSora Here is repo with reproduction of problem:
https://github.com/krutoo/jest-esm-overlayscrollbars-react

Just make npm install and npm run test

In my computer i have Node.js v20.11.0

@krutoo
Copy link
Author

krutoo commented Jan 23, 2024

@KingSora sorry for wrong link, I fixed it

@lpjune
Copy link

lpjune commented Jan 23, 2024

Also experiencing this issue with Node 20.11

@Smrtnyk
Copy link

Smrtnyk commented Jan 24, 2024

Do you need maybe "type": "module"?

@krutoo krutoo changed the title Add exports field to package.json Jest with Node.js v20.11.0 does not understand overlayscrollbars-react Jan 24, 2024
@KingSora
Copy link
Owner

KingSora commented Jan 24, 2024

@krutoo @lpjune @Smrtnyk I've identified the issue and will publish a fix soon :)

The problem is that even though the package jsons exports field looks like this:

"exports": {
  ".": {
    "require": "./overlayscrollbars-react.cjs.js",
    "import": "./overlayscrollbars-react.es.js",
    "types": "./types/overlayscrollbars-react.d.ts"
  }
}

Jest or node is reading the overlayscrollbars-react.es.js as a commonjs file. The reason being that I have no "type": "module" field and the fileextension of that file does not end with .mjs thus causing it to be read as a commonjs file.

@krutoo
Copy link
Author

krutoo commented Jan 24, 2024

@KingSora Please also note that the order of the keys matters according to the docs

Conditional exports:
https://nodejs.org/api/packages.html#conditional-exports

Node.js implements the following conditions, listed in order from most specific to least specific as conditions should be defined...

@Smrtnyk
Copy link

Smrtnyk commented Jan 24, 2024

if you set "type": "module" you should not need mjs as extension
node will read .js files as es modules by default

@KingSora
Copy link
Owner

@Smrtnyk Thats true, but it would also read the commonjs files as modules in that case

@ChristophP
Copy link

ChristophP commented Jan 24, 2024

Regaring the extensions

  • .mjs is always interpreted as ESM (regardless of the type field of the closes package json up the file tree)
  • .cjs is always interpreted as CJS (regardless of the type field of the closes package json up the file tree)
  • .js is interpreted as ESM or CJS depending on the type field of the closest package json up the file tree.

In that sense if you have type: module set, the ESM file can be called .js the CJS file .cjs. If you have no type module set or type: commonjs you can do .mjs for ESM and .js for CJS. If you wanna be absolutely sure regardless you can use .mjs/.cjs for them respectively.

@nickmccurdy
Copy link

I'd also recommend linting your package with publint.

@KingSora
Copy link
Owner

KingSora commented Jan 24, 2024

@krutoo I'll continue to provide commonjs and module versions because of backwardscompatibility reasons and because some frameworks / tools simply only support one or another but not always both (or the "right" one).

@ChristophP Thank you for the input - really appreciate it!

@nickmccurdy Very useful tool, I'll definitely use it before I publish the fixed versions - Thanks a bunch!

@KingSora
Copy link
Owner

I've published:

Which all address this issue and should hopefully fix it properly.
I've used https://publint.dev and https://arethetypeswrong.github.io/ to identifiy additional issues and fixed them where the error / warning was appropriate.

Please try it out and give feedback :)

@krutoo
Copy link
Author

krutoo commented Jan 24, 2024

@KingSora Yes, it helped in example repo and in my project, thank you so mush

Can you tell me what exactly you did to fix this problem?

Issue can be closed i think

@KingSora
Copy link
Owner

@krutoo I've changed the package.jsons exports field.. I've described the problem here: #604 (comment)

To see exactly how the I changed the files you can compare the old and new versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants