Angular 7: Uncaught ReferenceError: global is not defined when adding package

前端 未结 3 597
借酒劲吻你
借酒劲吻你 2020-12-17 10:58

I am building an Angular 7 app, and when i add a package npm install dragula --save and import this into the pollyfills.ts file i get this error:

3条回答
  •  星月不相逢
    2020-12-17 11:19

    Do you have custom webpack configuration for browser? Most probably declared in angular.json like so:

    ...
    "build": {
      "builder": "@angular-builders/custom-webpack:browser",
      "options": {
        "customWebpackConfig": {
        "path": "./webpack.browser.config.js"
      },
      ...
    

    Then webpack.browser.config.js might look like so:

    const dotenv = require("dotenv-webpack");
    const webpackConfig = {
      node: { global: true, fs: "empty" }, // Fix: "Uncaught ReferenceError: global is not defined", and "Can't resolve 'fs'".
      output: {
        libraryTarget: "umd", // Fix: "Uncaught ReferenceError: exports is not defined".
      },
      plugins: [new dotenv()],
      target: "node",
    };
    
    module.exports = webpackConfig; // Export all custom Webpack configs.
    

    Remove from its config target: "node" - it solves the problem.

提交回复
热议问题