javascript - Angular: utils/util.js -> Uncaught ReferenceError: process is not defined

标签 javascript angular webpack dependencies web3js

我觉得这应该简单地解决,但经过多次尝试,我就是没有找到解决方案。

这是我收到的错误:

Uncaught ReferenceError: process is not defined
38509 @ [PathToProject]\node_modules\util\util.js:109

当我将 web3 实例化到一个干净/新的站点时,这会被触发(还有两个其他“测试”组件,一个链接一个按钮)

我搜索并发现了大量信息表明

  • process 是一个服务器端“节点”var,我可以通过将其添加到我的 webpack.config.js 来将其设置为在客户端可用,我已经完成了。
  • 我也许可以通过在我的 app.component.ts 中声明一个全局 Angular 变量来解决,但似乎这个依赖项目 .js 文件没有访问它。

我也尝试过直接更新依赖项项目,但即使进行了编译,我的更改似乎也没有分发到 webpack build/dist/结果中。

我认为这可能有一个非常简单的解决方案,但我只是忽略了它。我只是在这里转动我的轮胎,需要一点帮助,但我是我圈子中第一个冒险进入 web3 并且没有亲密 friend 可以反弹的人。这里有人可以提供一些见解或替代方法来解决这个问题吗?

相关代码:

webpack.config.js

var webpack = require('webpack');
const path = require('path');


module.exports = {
    module: {
      rules: [
        {
          test: /\.(sass|less|css|ts)$/,
          use: [
            'ts-loader'
          ],
        }
      ],
    },
    plugins: [
        new webpack.DefinePlugin({
          'process.env.NODE_ENV': 'develop',
        })
    ],
    entry: './src/main.ts',
    output: {
      filename: 'main.js',
      path: path.resolve(__dirname, 'dist'),
    },
    resolve: {
      extensions: [ '.js', '.ts', '.html' ],
      modules: [
          path.resolve(__dirname, 'node_modules/'),
          path.resolve("", "src")
      ],
      alias: {
          Environments: path.resolve(__dirname, 'src/environments/'),
      },
      fallback: {
        "fs": false,
        "tls": false,
        "net": false,
        "path": false,
        "zlib": false,
        "http": require.resolve("stream-http"),
        "https": require.resolve("https-browserify"),
        "stream": false,
        "crypto": require.resolve("crypto-browserify"),
        "crypto-browserify": require.resolve('crypto-browserify'),  
    }, 
    }
}

全局常量.ts

export class GlobalConstants {
    public static process: any  = {
        env: {
            NODE_ENV: 'development'
        }
    }
}

应用程序组件.ts

import { Component } from '@angular/core';
import{ GlobalConstants } from './common/global-constants';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'Cool Title';
  process = GlobalConstants.process;
}

utils/util.js 的相关部分(第 106-116 行)

var debugs = {};
var debugEnvRegex = /^$/;

if (process.env.NODE_DEBUG) {
  var debugEnv = process.env.NODE_DEBUG;
  debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
    .replace(/\*/g, '.*')
    .replace(/,/g, '$|^')
    .toUpperCase();
  debugEnvRegex = new RegExp('^' + debugEnv + '$', 'i');
}

最佳答案

将以下内容添加到 polyfills.ts(通常在 src 目录中):

    (window as any).global = window;
    global.Buffer = global.Buffer || require('buffer').Buffer;
    global.process = require('process');

关于javascript - Angular: utils/util.js -> Uncaught ReferenceError: process is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70177438/

相关文章:

angular - 与 Angular 2 一起引用的 "ng2"是什么?

javascript - bundle.js 未在文件编辑时更新

css - 使用 create-react-app code-splitting 分离 CSS 文件而不弹出

vue.js - 托管应用程序后 npm run production 不起作用

javascript - 将 Select2 限制为仅一次搜索

javascript - JavaScript 中匹配 ${} 的正则表达式

JavaScript 引用数组插入

javascript - Mootools getElements ("input[type=radio]")返回对象而不是数组

大型项目列表的 Angular 2 性能优化

angular - 在嵌套子组件中使用辅助路由