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

The package may have incorrect main/module/exports specified in its package.json. when using import #2922

Closed
jdlien opened this issue Feb 27, 2023 · 9 comments

Comments

@jdlien
Copy link

jdlien commented Feb 27, 2023

Describe the bug

I'm the author of the @jdlien/validator package which has a test suite using vitest.

In the initial version of validator, it had a validator-utils.ts file that had utility functions in it. When the package was structured this way, vitest worked very well. I have since extracted this library to a separate package, @jdlien/validator-utils

In its current state, if you were to clone https://github.com/jdlien/validator and install its dependencies, you can build everything just fine using vite but doing npm run test will fail with the error

Error: Failed to resolve entry for package "@jdlien/validator-utils". The package may have incorrect main/module/exports specified in its package.json.

This implies that there's something wrong with @jdlien/validator-utils, which has a single module file it references which does indeed point to the correct file. validator-utils' package.json contains this

  "name": "@jdlien/validator-utils",
  "version": "1.1.5",
  "type": "module",
  "module": "dist/validator-utils.js",
  "types": "dist/validator-utils.d.ts",
  "files": [
    "dist"
  ]

It most definitely contains the dist/validator-utils.js and validator-utils.d.ts files. This is clear since the project does build with vite and the resulting js file does work fine in the browser.

Unfortunately since extracting the validator-utils to another package, I am unable to run tests in vitest and I get the following:

 FAIL  tests/Validator.test.ts [ tests/Validator.test.ts ]
Error: Failed to resolve entry for package "@jdlien/validator-utils". The package may have incorrect main/module/exports specified in its package.json.
 ❯ packageEntryFailure node_modules/vite/dist/node/chunks/dep-ca21228b.js:22005:11
 ❯ resolvePackageEntry node_modules/vite/dist/node/chunks/dep-ca21228b.js:22002:5
 ❯ tryNodeResolve node_modules/vite/dist/node/chunks/dep-ca21228b.js:21737:20
 ❯ Context.resolveId node_modules/vite/dist/node/chunks/dep-ca21228b.js:21488:28
 ❯ Object.resolveId node_modules/vite/dist/node/chunks/dep-ca21228b.js:41612:46
 ❯ TransformContext.resolve node_modules/vite/dist/node/chunks/dep-ca21228b.js:41363:23
 ❯ normalizeUrl node_modules/vite/dist/node/chunks/dep-ca21228b.js:39698:34
 ❯ TransformContext.transform node_modules/vite/dist/node/chunks/dep-ca21228b.js:39840:47
 ❯ Object.transform node_modules/vite/dist/node/chunks/dep-ca21228b.js:41685:30

Is this a bug in vitest? I have spent several hours trying variations of my configuration and of builds and am unable to get vitest to run with this project.

The only thing that makes a difference is if I change validator-utils to have a "main" property in its package.json, and I can compile a umd version of the library. In this case, instead of the may have incorrect main/module/exports error, I get hundreds of errors, one for every time the Validator class attempts to use one of the functions from validator-utils.

I'd appreciate it if someone could determine whether I'm doing something wrong in either validator or validator-utils or if I have indeed stumbled upon a genuine bug in vitest.

My packages seem to be building and working just fine in actual use but it's a shame I can't run the comprehensive test suite that I've developed using vitest to verify it.

Reproduction

git clone https://github.com/jdlien/validator
cd validator
npm i
npm run build
npm run tw
npm run test

(npm run build and npm run tw are only necessary to use the demo page, but you can view that the package works correctly by viewing it under demo/index.html.)

System Info

Attemped on macOS Monterey and Ventura
Under node v18 and node v19
npm v9.5.0
vite 4.1.4
vitest/0.29.1 darwin-x64 node-v18.14.2
@jdlien/validator 1.1.3
@jdlien/validator-utils 1.1.5
Have also tried yarn with no difference


Current environment (npx envinfo)

  System:
    OS: macOS 12.6.3
    CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
    Memory: 377.63 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.14.2 - /usr/local/bin/node
    npm: 9.5.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 110.0.5481.100
    Firefox: 98.0.1
    Safari: 16.3
  npmPackages:
    @vitest/coverage-c8: ^0.29.1 => 0.29.1
    vite: ^4.1.0 => 4.1.4
    vitest: ^0.29.1 => 0.29.1


### Used Package Manager

npm

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide/).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
@sheremet-va
Copy link
Member

You don't have "main" entry point. "module" entry point is ignored by Node.js, it is only supported by bundlers.

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2023
@jdlien
Copy link
Author

jdlien commented Feb 27, 2023

If I create a UMD and add it to a main entry point for the dependency, that will allow the tests to “run” but then they don’t see the functions added by the dependency and most of the tests fail, as mentioned in my issue.

@sheremet-va
Copy link
Member

If I create a UMD and add it to a main entry point for the dependency, that will allow the tests to “run” but then they don’t see the functions added by the dependency and most of the tests fail, as mentioned in my issue.

Why do you need to create a UMD, if you already have a valid ESM entry? Did you try putting your "module" into "main"?

@jdlien
Copy link
Author

jdlien commented Feb 27, 2023

I don’t. “need” to, but I tried this as an attempt to get vitest to run. It certainly changed the behavior.

I did actually have vite bundle the src files as both a module and a umd (the latter for the main entry point) but this doesn’t work well…

@sheremet-va
Copy link
Member

I don’t. “need” to, but I tried this as an attempt to get vitest to run. It certainly changed the behavior.

I did actually have vite bundle the src files as both a module and a umd (the latter for the main entry point) but this doesn’t work well…

If you have type: "module" your "main" entry point should have ESM file.

@jdlien
Copy link
Author

jdlien commented Feb 27, 2023

Interesting, I assumed that the main had to point to a UMD or something. I have tried adding the ESM file to both main and module, and the results in vitest are the same as with a UMD — hundreds of errors. Funny thing is that the project builds just fine, but the test environment fails miserably.

@jdlien
Copy link
Author

jdlien commented Feb 27, 2023

When I do have a main entry point, the failure that results looks like the following. I think maybe JSDOM can't see the functions from the import, but I don't understand why that would be.

> @jdlien/validator@1.1.3 test
> vitest


 DEV  v0.29.1 /Users/jdlien/code/validator

stderr | tests/Validator.test.ts > Validator > inputInputHandler > should parse integer input values
Error: Uncaught [TypeError: isType is not a function]
    at reportException (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
    at innerInvokeEventListeners (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:353:9)
    at invokeEventListeners (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3)
    at HTMLInputElementImpl._dispatch (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9)
    at HTMLInputElementImpl.dispatchEvent (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17)
    at HTMLInputElement.dispatchEvent (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34)
    at /Users/jdlien/code/validator/tests/Validator.test.ts:1643:19
    at file:///Users/jdlien/code/validator/node_modules/@vitest/runner/dist/index.js:131:13
    at file:///Users/jdlien/code/validator/node_modules/@vitest/runner/dist/index.js:40:26
    at runTest (file:///Users/jdlien/code/validator/node_modules/@vitest/runner/dist/index.js:439:15) TypeError: __vite_ssr_import_0__.isType is not a function
    at Validator.inputInputHandler (/Users/jdlien/code/validator/src/Validator.ts:584:15)
    at HTMLFormElement.callTheUserObjectsOperation (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30)
    at innerInvokeEventListeners (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25)
    at invokeEventListeners (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3)
    at HTMLInputElementImpl._dispatch (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9)
    at HTMLInputElementImpl.dispatchEvent (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17)
    at HTMLInputElement.dispatchEvent (/Users/jdlien/code/validator/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:241:34)
    at /Users/jdlien/code/validator/tests/Validator.test.ts:1643:19
    at file:///Users/jdlien/code/validator/node_modules/@vitest/runner/dist/index.js:131:13
    at file:///Users/jdlien/code/validator/node_modules/@vitest/runner/dist/index.js:40:26 {
  stackStr: 'TypeError: __vite_ssr_import_0__.isType is not a function\n' +
  ...

@jdlien
Copy link
Author

jdlien commented Feb 27, 2023

If anyone stumbles onto this with a similar issue, here's how I was finally able to fix it on my end.

First, I had to have a "main" line in the package.json for the dependency, otherwise vitest refused to run it and couldn't see the dependency package.

Second, in the vite.config.js configuration, I had to tell vitest to inline the dependency. With that, I was essentially able to solve all these problems and get my tests working again! 🎉

  test: {
    environment: 'jsdom',
    coverage: {
      reporter: ['text', 'json', 'html'],
    },
    threads: false, // suppresses errors from canvas when starting tests
    deps: {
      inline: ['@jdlien/validator-utils'],
    },
  }

@danielruxton
Copy link

I did in fact come across this and i can honestly say you are a saint and a scholar, my friend. Thank you very much.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants