nodejs

nodejs

Install

Ubuntu / Debian:

curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
apt-get install -y nodejs

CentOS / Red Hat

curl -sL https://rpm.nodesource.com/setup_18.x | sudo -E bash -
yum install -y nodejs

WSL

WSL1 里 npm 安装某些包可能报错 "npm ERR! Error: EACCES: permission denied"

Workaround: npm config set unsafe-perm=true

Tips & Solutions

Runtime memory limit (JavaScript heap out of memory error)

There is no official disclosed numbers regarding the default limits, but with a small program designed to push the process until it goes out of memory to capture the limits, I was able to get the following figures of default memory limits on a 64-bit system. (source)

Node.js Version   Limit  
----------------- -------
  20.x             4.0 GB
  19.x             4.0 GB
  18.x             4.0 GB
  17.x             4.0 GB
  16.x             4.0 GB
  15.x             4.0 GB  
  14.x             4.0 GB  
  13.x             2.0 GB  
  12.x             2.0 GB  
  11.x             1.4 GB  
  10.x             1.4 GB  
  9.x              1.4 GB

Fix:

# Set heap memory limit to 32768 MB
node --max-old-space-size=32768 index.js
// or
# Linux
export NODE_OPTIONS=--max-old-space-size=32768
# Windows cmd
set NODE_OPTIONS=--max_old_space_size=32768
node index.js

注:测试某些环境下 --max-old-space-size 选项无效,必须使用环境变量方式。

error:0308010C:digital envelope routines::unsupported

node v17+ 几乎 break 了所有之前版本环境的工具链。参看这里

Workaround:

#On Unix-like (Linux, macOS, Git bash, etc.):
export NODE_OPTIONS=--openssl-legacy-provider
#On Windows command prompt:
set NODE_OPTIONS=--openssl-legacy-provider
#On PowerShell:
$env:NODE_OPTIONS = "--openssl-legacy-provider"
# npm package.json scripts
# require cross-env // npm i --save-dev cross-env
cross-env NODE_OPTIONS='--openssl-legacy-provider' webpack build

Last update: 2024-01-15 01:56:52 UTC