Angular 6 many Can't resolve errors (crypto, fs, http, https, net, path, stream, tls, zlib)

后端 未结 8 2092
暖寄归人
暖寄归人 2020-11-28 05:56

I\'m building an Angular 6 app, but every time I want to serve to localhost, I get these errors:

ERROR in ./node_modules/aws-sign2/index.js
Module not found         


        
8条回答
  •  [愿得一人]
    2020-11-28 06:17

    You can user patch file solve this problem.

    patch.js

    const fs = require('fs');
    const f = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js';
    
    fs.readFile(f, 'utf8', function (err,data) {
      if (err) {
        return console.log(err);
      }
      var result = data.replace(/node: false/g, 'node: {crypto: true, stream: true}');
    
      fs.writeFile(f, result, 'utf8', function (err) {
        if (err) return console.log(err);
      });
    });
    

    package.json

    "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "node patch.js && ng build",
        "build-prod": "node patch.js && ng build --configuration=production",
        "build-staging": "node patch.js && ng build --configuration=staging"
    }
    

    make sure to "build" the code before ng serve only for the first time.

提交回复
热议问题