React项目npm启动报错:无法检测Node.js版本(Node 10.24.1)
Node.js 10.x 下 React 遗留项目启动报错:无法确定当前 Node 版本的解决方法
问题背景
- 运行环境:Node.js 10.24.1(无法升级)
- 操作流程:先执行
gulp run:backend启动后端,再执行gulp run:dar-360启动应用,控制台无报错,但浏览器无法加载应用 - 浏览器报错信息:
index.js:57 Uncaught TypeError: Unable to determine current node version at versionIncluded (index.js:57:1) at isCore (index.js:76:1) at ../../node_modules/resolve/lib/core.js (core.js:12:1) at __webpack_require__ (bootstrap:784:1) at fn (bootstrap:150:1) at ../../node_modules/resolve/index.js (index.js:3:1) at __webpack_require__ (bootstrap:784:1) at fn (bootstrap:150:1) at ../../node_modules/tslint/lib/utils.js (utils.js:25:1) at __webpack_require__ (bootstrap:784:1) at fn (bootstrap:150:1) at ./src/modules/maps/pages/map-page/components/help-modal.component.js (geo.helper.js:122:1) at __webpack_require__ (bootstrap:784:1) at fn (bootstrap:150:1) at ./src/modules/maps/pages/map-page/map.page.js (map.page.css:45:1) at __webpack_require__ (bootstrap:784:1) at fn (bootstrap:150:1) at ./src/components/app/app.component.js (app.component.css:45:1) at __webpack_require__ (bootstrap:784:1) at fn (bootstrap:150:1) at ./src/index.js (index.css:45:1) at __webpack_require__ (bootstrap:784:1) at fn (bootstrap:150:1) at 1 (service-worker.utils.js:56:1) at __webpack_require__ (bootstrap:784:1) at checkDeferredModules (bootstrap:45:1) at Array.webpackJsonpCallback [as push] (bootstrap:32:1) at main.chunk.js:1:67
解决方法
1. 锁定 resolve 包兼容版本
报错根源是resolve包的新版本不再兼容Node.js 10,其版本检测逻辑在旧Node环境下失效。需锁定到最后一个兼容Node.js 10的版本:
- 在项目
package.json中添加resolutions字段:"resolutions": { "resolve": "^1.17.0" } - 执行以下命令更新依赖:
npx npm-force-resolutions rm -rf node_modules package-lock.json npm install
2. 移除或降级 tslint
报错栈显示问题由tslint依赖resolve触发:
- 如果项目已不再需要tslint,直接卸载:
npm uninstall tslint - 若仍需使用,锁定tslint到兼容Node.js 10的版本(如
tslint@6.1.3),同时确保其依赖的resolve版本被上述resolutions锁定。
3. 修复Webpack的Node环境模拟
确保Webpack配置中正确模拟Node环境变量,避免浏览器端丢失Node版本信息:
在webpack.config.js中添加:
module.exports = { // ...其他配置项 node: { process: true, versions: { node: JSON.stringify(process.version) } } };
内容的提问来源于stack exchange,提问作者Myriam AA
相关产品推荐
相关产品推荐

