npm执行命令时报错:Cannot find module 'timers/promises'求解决方案
解决npm命令报错:Error: Cannot find module 'timers/promises'
问题描述
执行任意npm命令(包括尝试安装timers-promises的npm install timers-promises)时,均会抛出以下错误:
✘ username-2 ~/workplace/Project/src/Package ↱ git-branch ± npm install timers-promises [Sep-28|13:45] /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/es6/validate-engines.js:31 throw err ^ Error: Cannot find module 'timers/promises' Require stack: - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/@npmcli/agent/lib/util.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/@npmcli/agent/lib/index.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/remote.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/cache/entry.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/cache/index.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/fetch.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/index.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/npm-registry-fetch/lib/index.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/utils/replace-info.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/utils/error-message.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/utils/exit-handler.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/cli-entry.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/cli.js - /local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/bin/npm-cli.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) at Function.Module._load (internal/modules/cjs/loader.js:746:27) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:93:18) at Object.<anonymous> (/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/@npmcli/agent/lib/util.js:3:16) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at Module.require (internal/modules/cjs/loader.js:974:19) { code: 'MODULE_NOT_FOUND', requireStack: [ '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/@npmcli/agent/lib/util.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/@npmcli/agent/lib/index.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/remote.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/cache/entry.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/cache/index.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/fetch.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/make-fetch-happen/lib/index.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/node_modules/npm-registry-fetch/lib/index.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/utils/replace-info.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/utils/error-message.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/utils/exit-handler.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/cli-entry.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/lib/cli.js', '/local/home/username/.nvm/versions/node/v14.18.3/lib/node_modules/npm/bin/npm-cli.js' ] }
已尝试的无效操作
- 通过nvm安装不同版本的Node.js(14、16、20)及对应npm,错误仍存在
- 在x86_64、aarch64两种架构的Amazon Linux机器上测试,均出现相同问题
问题原因
timers/promises是Node.js 15.0.0及以上版本才内置的核心模块,你当前使用的Node.js 14.18.3不支持该模块。报错中的@npmcli/agent是npm的依赖模块,若你的npm版本过高(如npm 8+),其依赖链会引入要求Node.js 15+的代码,导致在Node.js 14环境下运行失败。
解决思路与步骤
1. 安装与Node.js 14.18.3兼容的npm版本
Node.js 14.18.3默认配套的npm版本为6.14.18,这个版本的依赖不会引入timers/promises:
# 切换到指定Node.js版本 nvm use 14.18.3 # 全局安装兼容的npm版本 npm install -g npm@6.14.18
2. 清理缓存并重新安装Node.js(若上述步骤无效)
可能是nvm缓存或残留文件导致的问题,执行以下操作:
# 清理npm缓存 npm cache clean --force # 卸载当前Node.js 14.18.3版本 nvm uninstall 14.18.3 # 重新安装Node.js 14.18.3(会自动安装配套的npm 6.14.18) nvm install 14.18.3
3. 排查全局npm配置干扰
检查用户目录下的.npmrc文件,若存在可能影响依赖加载的配置(如自定义registry、proxy等),可暂时将其重命名为.npmrc.backup,再尝试执行npm命令。
内容的提问来源于stack exchange,提问作者y2k-shubham
相关产品推荐
相关产品推荐

