浏览器错误:未捕获类型错误:无法确定bundle.js中的当前节点版本

问题描述 投票:0回答:1

所以我最近在我的应用程序中安装了 browserify,并且在 Chrome 开发控制台中收到此错误:

bundle.js:10677 Uncaught TypeError: Unable to determine current node version
    at versionIncluded (bundle.js:10677:9)
    at isCore (bundle.js:10694:28)
    at 71../core.json (bundle.js:15883:21)
    at o (bundle.js:1:265)
    at bundle.js:1:316
    at 67../lib/async (bundle.js:15523:14)
    at o (bundle.js:1:265)
    at bundle.js:1:316
    at Object.<anonymous> (bundle.js:6817:12)
    at Object.<anonymous> (bundle.js:7158:4)

这是我的应用程序中与错误相关的代码:



    var current = typeof nodeVersion === 'undefined'
        ? process.versions && process.versions.node
        : nodeVersion;

    if (typeof current !== 'string') {
        throw new TypeError(typeof nodeVersion === 'undefined' ? 'Unable to determine current node version' : 'If provided, a valid node version is required');
    }

所以,基本上,当它无法获取节点版本时,它会抛出一个错误,它不会为 current 赋值,稍后会抛出一个错误。我有 Node v20.6.1,所以我不明白为什么 nodeVersion 未定义或如何修复它。我猜测我的应用程序应该在构建 webpack 时传递该值,但由于某种原因没有正确构建它。但是,我正在运行正确的命令来构建 webpack:

 browserify scripts.js -o bundle.js

如有任何指导,我们将不胜感激。

我尝试删除bundle.js并重建它。我也尝试过注释掉一般的错误处理,但它需要节点版本为以后分配一个值给当前。

javascript node.js webpack browserify
1个回答
0
投票

答案已发布这里

显然,browserify 在编译时注入了变量,因此全局变量和进程变量是不同的。 Global是节点,process是浏览器进程。我通过将 process.versions 和 process.versions.node 分别更改为 global.process.versions global.process.versions.node 解决了该问题。这是新代码:

  console.log(global.process.versions.node)
    var current = typeof nodeVersion === 'undefined'
        ? global.process.versions && global.process.versions.node
        : nodeVersion;
© www.soinside.com 2019 - 2024. All rights reserved.