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

[BUG] pnpm build failed #269

Closed
ezrawic4j opened this issue Oct 13, 2023 · 9 comments
Closed

[BUG] pnpm build failed #269

ezrawic4j opened this issue Oct 13, 2023 · 9 comments
Labels
bug Something isn't working

Comments

@ezrawic4j
Copy link

✓ 781 modules transformed.
rendering chunks (28)...The emitted file "lang-5a385cdb.js" overwrites a previously emitted file of the same name.
The emitted file "langSign-66e8939d.js" overwrites a previously emitted file of the same name.
The emitted file "pluralPolyfill-61f068d6.js" overwrites a previously emitted file of the same name.
The emitted file "countries-5301fc59.js" overwrites a previously emitted file of the same name.
The emitted file "lang-5a385cdb.js.map" overwrites a previously emitted file of the same name.
The emitted file "langSign-66e8939d.js.map" overwrites a previously emitted file of the same name.
The emitted file "pluralPolyfill-61f068d6.js.map" overwrites a previously emitted file of the same name.
The emitted file "countries-5301fc59.js.map" overwrites a previously emitted file of the same name.
[vite:build-import-analysis] Cannot read properties of undefined (reading 'forEach')
✓ built in 39.95s
error during build:
TypeError: Cannot read properties of undefined (reading 'forEach')
at addDeps (file:///home/tweb/node_modules/.pnpm/vite@4.3.1_@types+node@18.11.18_sass@1.64.2/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45215:55)
at Object.generateBundle (file:///home/tweb/node_modules/.pnpm/vite@4.3.1_@types+node@18.11.18_sass@1.64.2/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45236:33)
at file:///home/tweb/node_modules/.pnpm/rollup@3.20.7/node_modules/rollup/dist/es/shared/node-entry.js:24412:40
 ELIFECYCLE  Command failed with exit code 1.

image
@ezrawic4j ezrawic4j added the bug Something isn't working label Oct 13, 2023
@Leev1s
Copy link

Leev1s commented Nov 12, 2023

Same issue

@slavabarkal
Copy link

someone have solution for this error?

@KarafiziArtur
Copy link

KarafiziArtur commented Nov 24, 2023

@morethanwords @ezrawic4j
We have faced the same issue. Is there any solution?

I've found that some of the chunks have no imports field array as lang.ts and that's why the chunk.imports.forEach method in the mentioned vite file doesn't exist and throwing an error due to the field is undefined

Screenshot 2023-11-24 at 10 39 21

And when I fixed the file by adding conditions - the build has been successful

So maybe the problem with the chunk generation or something else

@morethanwords
Copy link
Owner

So maybe the problem with the chunk generation or something else

It is. The bug is inside the vite. In order to build it, conditionals should be added in vite/dist/node/chunks/dep-24daf00c.js on lines 45215 and 45218 this way:

image

@KarafiziArtur
Copy link

So maybe the problem with the chunk generation or something else

It is. The bug is inside the vite. In order to build it, conditionals should be added in vite/dist/node/chunks/dep-24daf00c.js on lines 45215 and 45218 this way

I see, thanks. As I understand it's your official workaround for this bug. Can you write an issue to the specific package (rollup/vite) to fix the problem from the root?

@bufanyun
Copy link

Compilation error reported:

PS \tweb> vite build
has built solid true
vite v4.3.1 building for production...
✓ 1111 modules transformed.
rendering chunks (30)...The emitted file "lang-49055ff2.js" overwrites a previously emitted file of the same name.
The emitted file "pluralPolyfill-61f068d6.js" overwrites a previously emitted file of the same name.
The emitted file "countries-5301fc59.js" overwrites a previously emitted file of the same name.
The emitted file "langSign-66e8939d.js" overwrites a previously emitted file of the same name.
The emitted file "lang-49055ff2.js.map" overwrites a previously emitted file of the same name.
The emitted file "pluralPolyfill-61f068d6.js.map" overwrites a previously emitted file of the same name.
The emitted file "langSign-66e8939d.js.map" overwrites a previously emitted file of the same name.
[vite:build-import-analysis] Cannot read properties of undefined (reading 'forEach')
✓ built in 48.32s
error during build:
TypeError: Cannot read properties of undefined (reading 'forEach')
    at addDeps (/tweb/node_modules/.pnpm/vite@4.3.1_@types+node@18.11.18_sass@1.64.2/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45215:55)
    at Object.generateBundle (/tweb/node_modules/.pnpm/vite@4.3.1_@types+node@18.11.18_sass@1.64.2/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45239:33)
    at /tweb/node_modules/.pnpm/rollup@3.20.7/node_modules/rollup/dist/es/shared/node-entry.js:24412:40

Solution: Find the error line in the dep-24daf00c.js file and modify it:

Aboriginal:

chunk.imports.forEach(addDeps);

// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
chunk.viteMetadata.importedCss.forEach((file) => {
    deps.add(file);
});

Modified:

if (chunk.imports){
    chunk.imports.forEach(addDeps);
}

// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
if (chunk.viteMetadata && chunk.viteMetadata.importedCss){
    chunk.viteMetadata.importedCss.forEach((file) => {
        deps.add(file);
    });
}

Finally, run the 'vite build' again and compile it successfully.

https://github.com/morethanwords/tweb/issues/269#issuecomment-1825082899

@SimonsUnikaNamn
Copy link

Hello @morethanwords, I am having this issue when trying to migrate quite a huge frontend project from CRA to Vite and this is the only things stopping me currently

Do you know if they will approve and let your fix into production?

@morethanwords
Copy link
Owner

Do you know if they will approve and let your fix into production?

Not sure yet, you could also ask them here vitejs/vite#15469, maybe they will change the priority from minor.

@SimonsUnikaNamn
Copy link

Thanks for answering, I have found a solution on my end

I used "build": "vite build --minify false", in order to not minimize the code
in node_modules I applied your fix and console logged the files that did not have imports
By looking into the files in the dist I was able to deduce where they belonged, in my case it came from dependencies in jszip

In my vite.config.ts I added manual chunking to make sure they were added into one chunk

build: {
    outDir: "build",
    target: "es2020",
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("jszip") || id.includes("pako")) {
            return "jszip";
          }
          if (id.includes("node_modules")) {
            return "vendor";
          }

          return "index";
        },
      },
    },
  },```

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

No branches or pull requests

7 participants