1

I am running a build for a large project using pnpm and Vite, but it keeps failing with out of memory errors.

I have already tried increasing the Node memory limit significantly (to ~12GB), but it still crashes:

NODE_OPTIONS="--max-old-space-size=12288" pnpm build

I'm doing a whole bunch of dynamic imports and manual chunking. I've also stripped my vite.config.js down to the bare minimum to rule out plugin issues.

FYI I cannot disable sourcemap generation (i.e. setting sourcemap: false is not an option as I need them for debugging).

Are there other specific Vite configurations, Rollup options, or pnpm strategies I can use to decrease the memory footprint during the build?

asked Jan 2 at 15:41
2
  • Did you try to switch to npm to see if this makes any difference? I'd expect there will be none Commented Jan 3 at 17:00
  • @EstusFlask Tried that. Didn't make a difference, as expected :( Commented 9 hours ago

1 Answer 1

0

You can use esbuild minifier. esbuild minification is Vite’s default for dev and recommended for production for speed and memory efficiency.

build: {
 target: 'esnext',
 minify: 'esbuild',
 sourcemap: true,
}

Also, you should configure Vite’s disk cache (cacheDir) to avoid redundant work in rebuilds:

build: {
 cacheDir: '.vite-cache',
}

You should also consider using import() calls instead of having large sync imports:

const BigComponent = () => import('./BigComponent.vue') instead of import BigComponent from './BigComponent.vue'

And finally, try to use build.rollupOptions.output.manualChunks to break your bundle into smaller pieces. Moving heavy vendors like React, lodash, into separate chunks reduces peak memory pressure during build.

vite.config.js

import { defineConfig } from 'vite'
export default defineConfig({
 build: {
 rollupOptions: {
 output: {
 manualChunks(id) {
 if (id.includes('node_modules')) {
 if (id.includes('react') || id.includes('react-dom')) return 'react-vendor'
 if (id.includes('lodash') || id.includes('date-fns')) return 'utils-vendor'
 return 'vendor'
 }
 },
 },
 },
 },
})
answered Jan 2 at 15:54
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. I should have mentioned in the OP that I'm already doing a whole bunch of dynamic imports and manual chunking. As for esbuild minification, I think esbuild is the default as setting minify to it explicitly made no difference at all. And cacheDir isn't super useful for me as my builds happen on on-demand VMs with no persistence.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.