-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
vite build error: out of memory #2433
Comments
Update It worked when I allocated a whopping 16GB of RAM for Vite's initialization cycle. As a temporary workaround, you can use the following commands. If it still fails, just increase the size. Temporary workarounda. Using command line arguments
b. Using environment variables
I'm getting the same error on https://github.com/inkline/inkline/tree/inkline3 after updating from It looks like a memory leak to me. I've tried starting from a clean project and gradually added my code to it in order to find the issue. I discovered that it only happens when I I tried increasing the memory allocation size to 9GB, but it didn't do the trick.
Randomly, I also get this error:
|
This comment was marked as off-topic.
This comment was marked as off-topic.
For me at least, Vite was compiling my tests coverage folder which led to this heap error so I had to specify |
I could only get it to work with node 15 on an Apple M1 without having to increase the |
It turns out I also had a coverage directory that was the cause of this issue. Either way, it would be good to have some sort of exclude option in the Vite config for this. Edit: I wonder if it's due to there being an |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@samuveth You should workaround this by using |
Also, just set it as an env variable and you don't have to call Vite's js script directly: # where xxxx = (your memory in GiB) * 1024 - 512
export NODE_OPTIONS='--max-old-space-size=xxxx' |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as off-topic.
This comment was marked as off-topic.
contributions are welcome |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as off-topic.
This comment was marked as off-topic.
For me, I was experiencing memory errors because of some heavy libraries used in my React application. I observed Vite was transforming over 7000 files every time I added these heavy libraries to the production build process. To fix it, I had to find alternative ways to import those libraries or use their minified versions or import just the needed sub-packages for libraries with sub-packages like _lodash. Lastly, disabling the source map in the Vite config reduced the memory needed to build my application by more than 500MB. |
This comment was marked as off-topic.
This comment was marked as off-topic.
I see some people are encountering this with Using the |
Perhaps counting the cpus helps? import { cpus } from "os";
maxParallelFileOps: Math.max(1, cpus().length - 1), |
@samelie I tried that and it didn't work. P.s: for some reason the problem I had automatically got fixed. I feel gh actions were catching something and it got fixed automatically. I am still confused tho! |
I had this issue when I was building vite through a github action. As a temporary fix, you can add to NODE_OPTIONS the flag: with a larger specification than the one it failed on (for me 'max-old-space-size=4096' did the trick). This can be done by creating a .npmrc file at the root of your project (where the package.json usually resides) and adding the following line in it: If you are not sure that the memory changed, you can check it by creating a script that runs the following: ` console.table(v8.getHeapStatistics()); and running it as part of your github action steps after the installation phase Hope that helps! |
I finally found a fix on my react project which was legacy code base that I migrated from react rewired to vite. I tried all the hacks explained above and none seamed to work. After comparing with other projects I realised compile options in Here is my working
and this is my tsconfig.node.json
and finally
|
@pinchez254 Can you elaborate on what |
@pinchez254 I compared your tsconfig.json with mine. What worked for me was simply changing |
An option that worked for me, even though it not perfect is the following. For me the packages that broke Vite + Rollup were one of So, after some optimization on function differMuiSourcemapsPlugins() {
const muiPackages = ["@mui/material", "@emotion/styled", "@emotion/react"];
return {
name: "differ-mui-sourcemap",
transform(code: string, id: string) {
if (muiPackages.some((pkg) => id.includes(pkg))) {
return {
code: code,
map: null,
};
}
},
};
} And here is my partial ViteConfig, the parts that matter: export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd());
return {
plugins: [
react(),
eslint(),
createHtmlPlugin({
inject: {
data: env,
},
}),
differMuiSourcemapsPlugins(),
],
...,
build: {
outDir: "build",
sourcemap: mode === "development" || "hidden",
rollupOptions: {
maxParallelFileOps: Math.max(1, cpus().length - 1),
output: {
manualChunks: (id) => {
if (id.includes("node_modules")) {
return "vendor";
}
},
sourcemapIgnoreList: (relativeSourcePath) => {
const normalizedPath = path.normalize(relativeSourcePath);
return normalizedPath.includes("node_modules");
},
},
cache: false,
},
},
};
}); Hope that helps some people. Tbh, I would like the underlying issue to be fixed in general as I tried a lot of different vite versions and none actually worked. Have a nice day! |
Any updates about this bug? I've got this issue too (in GH actions), but none of the methods above worked. This bug unreproducible on local machine, and GH logs don't say something about the reason of the problem. |
I got GH actions working by installing
|
We are testing out a plugin inspired by @EfstathiadisD's comment that disables source maps for all node modules instead of just a subset. |
Thanks @EfstathiadisD for charting the course. Confirming this resolved all the problems. import type { Plugin } from "vite";
interface SourcemapExclude {
excludeNodeModules?: boolean;
}
export function sourcemapExclude(opts?: SourcemapExclude): Plugin {
return {
name: "sourcemap-exclude",
transform(code: string, id: string) {
if (opts?.excludeNodeModules && id.includes("node_modules")) {
return {
code,
// https://github.com/rollup/rollup/blob/master/docs/plugin-development/index.md#source-code-transformations
map: { mappings: "" },
};
}
},
};
}
// USAGE
sourcemapExclude({ excludeNodeModules: true }), |
I find a reason of problem in my case. It's import of elkjs library by the following way presented in their docs for typescript usage |
In case it helps someone else, what finally worked for us:
All the other various workarounds listed in the comments didn't resolve the issue. Something that helped debug the issue was building the project in WSL, I couldn't reproduce the issue when running build locally on windows but could do so when building the project through wsl. |
Thank your solution. |
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as off-topic.
This comment was marked as off-topic.
@patak-dev I wonder if perhaps this issue should be considered an upstream problem with rollup, and closed here, since there is a lot of noise and I think everything that can be said, has been said. In the end, there's nothing I think Vite can do to solve this issue. |
Even if it is upstream, it is an important issue for a lot of folks so it makes sense to keep tracking it here. I'm afraid people will create new issues if we close this one. Let's keep it open but read-only to avoid generating so many notifications to everybody until it is solved in rollup. |
NOTE - PLEASE READ
This issue has affected a number of people. Please do not comment with comments along the lines of "I'm also hitting this", but add a thumbs up to the issue instead. We know this is an important issue and "me too" comments will only drown out the comments from people attempting to resolve this issue and will be hidden. Please note however that Vite is a community driven project and a fix may need to come from the community
For tips on working around the issue, please see https://rollupjs.org/guide/en/#error-javascript-heap-out-of-memory
Describe the bug
vite build error: out of memory.
Reproduction
https://github.com/Axeldeblen/realworld-big-build
Possible improvements
Reduce magic string memory usage when building source maps: Rich-Harris/magic-string#161 (comment)
Logs (Optional if provided reproduction)
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 00007FF69790ABBD]
Security context: 0x01e6a86408d1
1: decode(aka decode) [000002AD02F874D1] [E:\vite-template\node_modules_rollup@2.40.0@rollup\dist\shared\rollup.js:~133] [pc=0000039464A55451](this=0x037824a004b1 ,0x017863480119 <Very long string[1502653]>)
2: decodedSourcemap(aka decodedSourcemap) [000002AD02F8A979] [E:\vite-template\node_modules_rollup@2.40.0@rollup\dist\shared\roll...
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 00007FF696CD180F napi_wrap+119407
2: 00007FF696C787E6 v8::internal::OrderedHashTablev8::internal::OrderedHashSet,1::NextTableOffset+38102
3: 00007FF696C795E6 node::OnFatalError+438
4: 00007FF6974B5A6E v8::Isolate::ReportExternalAllocationLimitReached+94
5: 00007FF69749DC21 v8::SharedArrayBuffer::Externalize+833
6: 00007FF69734F3FC v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436
7: 00007FF69735A640 v8::internal::Heap::ProtectUnprotectedMemoryChunks+1312
8: 00007FF697357154 v8::internal::Heap::PageFlagsAreConsistent+3204
9: 00007FF69734C953 v8::internal::Heap::CollectGarbage+1283
10: 00007FF69734AFC4 v8::internal::Heap::AddRetainedMap+2500
11: 00007FF69736C30D v8::internal::Factory::NewFillerObject+61
12: 00007FF6970CF76F v8::internal::interpreter::JumpTableTargetOffsets::iterator::operator=+1295
13: 00007FF69790ABBD v8::internal::SetupIsolateDelegate::SetupHeap+546925
14: 0000039464A55451
The text was updated successfully, but these errors were encountered: