树莓派Zero 2 W安装Node.js的ws模块报错求助
树莓派Zero 2 W安装ws模块的npm报错修复方案
问题场景
树莓派Zero 2 W上运行Node.js v15.8.0,执行sudo npm i ws时触发以下报错:
julius@pi:~ $ sudo npm i ws TypeError: Class extends value #<Object> is not a constructor or null at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/tar/lib/read-entry.js:90:4) at Module._compile (node:internal/modules/cjs/loader:1108:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10) at Module.load (node:internal/modules/cjs/loader:973:32) at Function.Module._load (node:internal/modules/cjs/loader:813:14) at Module.require (node:internal/modules/cjs/loader:997:19) at require (node:internal/modules/cjs/helpers:92:18) at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/tar/lib/pack.js:27:19) at Module._compile (node:internal/modules/cjs/loader:1108:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10) /usr/lib/node_modules/npm/node_modules/@npmcli/config/lib/index.js:163 throw new Error('call config.load() before reading values') ^ Error: call config.load() before reading values at Config.get (/usr/lib/node_modules/npm/node_modules/@npmcli/config/lib/index.js:163:13) at process.errorHandler (/usr/lib/node_modules/npm/lib/utils/error-handler.js:171:32) at process.emit (node:events:378:20) at process._fatalException (node:internal/process/execution:163:25) julius@pi:~ $
修复步骤
方案一:升级npm版本
Node.js v15.8.0自带的npm与依赖包存在兼容性问题,先升级npm:
sudo npm install -g npm@latest
升级完成后重新执行npm i ws(无需sudo,避免系统权限冲突)。
方案二:用nvm管理Node.js版本(推荐)
nvm可避免系统级Node/npm环境的冲突,操作步骤:
- 安装nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
- 重新加载shell配置:
source ~/.bashrc
- 安装稳定兼容的Node.js版本(如v16.x):
nvm install 16 nvm use 16
- 安装ws模块:
npm i ws
方案三:降级tar依赖包(临时修复)
若不想升级Node/npm,可手动降级npm的tar依赖:
cd /usr/lib/node_modules/npm sudo npm install tar@6.1.11
完成后再执行sudo npm i ws。
内容的提问来源于stack exchange,提问作者JFT
相关产品推荐
相关产品推荐

