javascript 如何为typescript编译器(tsc)设置max-old-space-size?

3duebb1j  于 2023-06-04  发布在  Java
关注(0)|答案(3)|浏览(248)

我在gitlab ci上的tsc作业失败,使用FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
我运行的脚本是tsc -p test/tsconfig.json --noEmit
到目前为止,我尝试的是通过以下方式增加max-old-space-size

  1. cross-env NODE_OPTIONS=--max-old-space-size=8192 yarn print:heapsize && tsc -p test/tsconfig.json --noEmit
  2. cross-env NODE_OPTIONS='--max-old-space-size=8192' yarn print:heapsize && tsc -p test/tsconfig.json --noEmit(var周围的额外引号)
  3. node --max-old-space-size=8192 ./node_modules/.bin/tsc -p test/tsconfig.json --noEmit
    选项#1和#2不起作用,选项#3工作正常,但运行二进制文件并将param传递给node cli听起来不太好

所以我的问题是-如何通过env variablesmax-old-spce-size设置为tsc,或者可能有另一种好方法来做到这一点?

yarn print:heapsize是我用来检查当前大小的脚本-它显示#1和#2都是8 gb,如下所示:
"print:heapsize": "node -e \"console.log('🔥 Current heap size:', (require('v8').getHeapStatistics().total_available_size / 1024 / 1024 / 1024).toFixed(2), 'Gb')\""
这里是输出x1c 0d1x

m528fe3b

m528fe3b1#

前两个选项失败,因为环境变量NODE_OPTIONS=--max-old-space-size=8192仅应用于&&之前的第一个命令(yarn print:heapsize)。
如果删除yarn print:heapsize &&,环境将按预期应用于编译命令:
cross-env NODE_OPTIONS=--max-old-space-size=8192 tsc -p test/tsconfig.json --noEmit

kqlmhetl

kqlmhetl2#

如果你只想用更多的内存来运行tsc,你可以通过npx和新的node-options arg来运行它:

npx --node-options="--max-old-space-size=16000" tsc

我们有一个非常大的代码库和tsc运行内存快。我们只使用它进行预签入类型检查。只是为了看看在测试中是否有任何地方编译失败。
这个选项似乎工作得很好,如果你有足够的RAM安装,你可以碰撞到尽可能多的内存。
请记住,在非常大的代码库上,这可能需要10分钟以上的时间来运行,或者更长。

zc0qhyus

zc0qhyus3#

tsc已弃用,但您可以在NODE_OPTIONS中将ts-node--max-old-space-size一起使用
package.json中添加此选项

{
  "scripts": {
    "start": "NODE_OPTIONS='--max-old-space-size=8192' ts-node index.ts"
   }
}

相关问题