node内存的限制

node运行时会有一个默认的内存限制,当node操作的数据量过大时就会抛出异常。一般有个这样的说法,32位机器上node内存限制512M,64位机器上限制内存1G。但是node也给出了用户手动配置此界限的方法。例如:

Luckily node let’s you up the memory limit with the archaically named --max-old-space-size=SPACE_IN_MB flag. And to make life even easier, it allows you to globally set these options in the environment, so you don’t need to worry about catching every node invocation yourself: export NODE_OPTIONS=--max-old-space-size=8192 (for example).

或者

Starting nodejs app with a heap memory of 8 GB

node --max-old-space-size=8192 app.js

使用nvm对node的版本做管理

随着项目的增加,node的版本在很多时候成了各种bug产生的根本原因。我们需要使用nvm对node版本做管理和使用。

以下基本用法:

To download, compile, and install the latest release of node, do this:

nvm install node # "node" is an alias for the latest version

To install a specific version of node:

nvm install 6.14.4 # or 10.10.0, 8.9.1, etc

The first version installed becomes the default. New shells will start with the default version of node (e.g., nvm alias default).

You can list available versions using ls-remote:

nvm ls-remote

And then in any new shell just use the installed version:

nvm use node

Or you can just run it:

nvm run node --version

Or, you can run any arbitrary command in a subshell with the desired version of node:

nvm exec 4.2 node --version

You can also get the path to the executable to where it was installed:

nvm which 5.0