4

When trying to build the app with Vite I'm seeing an error. If I understand it correctly there seems to be an issue with Yarn PnP resolving dependencies (no more node_modules), and Vite does not seem to pick up on this? How can I make Vite understand that node_modules no longer exist?

Update: Reproduced test case here: https://github.com/michaeljohansen/vite-test-case - Error seems to go away if removing "type": "module" from package.json, but that makes no sense to me yet, and creates other problems for my Node backend.

$ yarn run vite --config vite.config.js
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'vite' imported from /Users/me/project/vite.config.js
Did you mean to import vite-virtual-ec56a6c02a/0/cache/vite-npm-2.9.13-cda1bb45b9-a5e501b920.zip/node_modules/vite/dist/node/index.js?
    at new NodeError (node:internal/errors:377:5)
    at packageResolve (node:internal/modules/esm/resolve:910:9)
    ...
// vite.config.js excerpt
import { defineConfig } from 'vite';

export default defineConfig(({ command, mode }) => {
  ...
});
Michael Johansen
  • 4,688
  • 5
  • 29
  • 47
  • I cannot reproduce the issue. Can you share a link to a reproduction of the problem? – tony19 Jul 10 '22 at 19:41
  • 1
    Absolutely, here you go: https://github.com/michaeljohansen/vite-test-case - Also, the error seems to go away if removing `"type": "module"` from `package.json`, but that makes no sense to me yet. – Michael Johansen Jul 10 '22 at 23:05
  • If I set yarn to 3.1.1, I get a different error related to `pnp.cjs`. On the other hand, setting yarn to the latest berry version (3.2.1), no errors occur at all. Is there a reason you're still on 3.1? – tony19 Jul 11 '22 at 00:28
  • The same thing happened with Yarn v3.2.1 unfortunately. – Michael Johansen Jul 13 '22 at 20:20
  • What's your environment? Mine: macOS Big Sur, Node 17.4.0 – tony19 Jul 13 '22 at 21:10
  • macOS Monterey, M1 processor, Node 18.5.0, Yarn 3.2.1, Vite 2.9.13. – Michael Johansen Jul 14 '22 at 16:56
  • That repo works just for me with Node 18.3 and 18.5. Fails on 18.6. Can you include your .pnp.cjs and .yarnrc.yml in there? – Anton Mihaylov Jul 15 '22 at 20:39
  • 1
    It turns out that I needed to upgrade Yarn to 3.2.1, but I had forgotten to update the `packageManager` field in `package.json` with Yarn 3.2.1. Bounty awarded to answer that told me to upgrade Yarn version. Also, pushed .pnp.cjs now @AntonMihaylov, I don't have a .yarnrc.yml for this repo yet. – Michael Johansen Jul 16 '22 at 21:28

1 Answers1

5

You need to upgrade to yarn 3.2.0 or above.

yarn set version stable 
# or 
yarn set version 3.2.0

And run yarn install to update the .pnp.cjs. See more at the Change log that they have handle the ESM and commonjs modules.

Rex Pan
  • 1,458
  • 10
  • 13
  • Unfortunately still doesn't work. It only starts working if I remove `"type": "module"` from `package.json`, but that creates problems for my Node backend so not really an option. I had to stop using Yarn PnP and go back to using `nodeLinker: node-modules` in my `.yarnrc.yml`. Node 18.5.0, Yarn 3.2.1, Vite 2.9.13. – Michael Johansen Jul 14 '22 at 16:55
  • Update: Seems it works when I also changed the version of Yarn in `package.json`, so with that your answer seems to be correct. Awarding the bounty now. Thanks! – Michael Johansen Jul 16 '22 at 21:22
  • 1
    what did you change the version to? this does not work for me... – Zaffer Dec 15 '22 at 17:00