node.js Angular 6 许多无法解决的错误(crypto、fs、http、https、net、path、stream、tls、zlib)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51087330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:51:04  来源:igfitidea点击:

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

node.jsangularnpmwebpackangular6

提问by Zizazorro

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

我正在构建一个 Angular 6 应用程序,但每次我想为本地主机提供服务时,我都会收到以下错误:

ERROR in ./node_modules/aws-sign2/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\aws-sign2'
ERROR in ./node_modules/aws4/aws4.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\aws4'
ERROR in ./node_modules/ecc-jsbn/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\ecc-jsbn'
ERROR in ./node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\http-signature\lib'
ERROR in ./node_modules/http-signature/lib/verify.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\http-signature\lib'
ERROR in ./node_modules/oauth-sign/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\oauth-sign'
ERROR in ./node_modules/request/lib/oauth.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request\lib'
ERROR in ./node_modules/request/lib/helpers.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request\lib'
ERROR in ./node_modules/request/lib/hawk.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request\lib'
ERROR in ./node_modules/sshpk/lib/signature.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/private-key.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/certificate.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/fingerprint.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/key.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/dhe.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/identity.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/utils.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/formats/pem.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/sshpk/lib/formats/ssh-private.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/sshpk/lib/formats/openssh-cert.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request\lib'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'http' in 'C:\Users\sorou\projects\tunrWeb\node_modules\forever-agent'
ERROR in ./node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'http' in 'C:\Users\sorou\projects\tunrWeb\node_modules\http-signature\lib'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'http' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'http' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tunnel-agent'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'https' in 'C:\Users\sorou\projects\tunrWeb\node_modules\forever-agent'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'https' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'https' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tunnel-agent'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\sorou\projects\tunrWeb\node_modules\forever-agent'
ERROR in ./node_modules/tough-cookie/lib/cookie.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tough-cookie\lib'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tunnel-agent'
ERROR in ./node_modules/mime-types/index.js
Module not found: Error: Can't resolve 'path' in 'C:\Users\sorou\projects\tunrWeb\node_modules\mime-types'
ERROR in ./node_modules/assert-plus/assert.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\assert-plus'
ERROR in ./node_modules/combined-stream/lib/combined_stream.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\combined-stream\lib'
ERROR in ./node_modules/delayed-stream/lib/delayed_stream.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\delayed-stream\lib'
ERROR in ./node_modules/isstream/isstream.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\isstream'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request'
ERROR in ./node_modules/sshpk/lib/ed-compat.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\sorou\projects\tunrWeb\node_modules\forever-agent'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tunnel-agent'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'zlib' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request'

This is my package.json:

这是我的 package.json:

{
  "name": "tunr-web",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.7",
    "@angular/cdk": "github:angular/cdk-builds",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/material": "github:angular/material2-builds",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "angular-svg-round-progressbar": "^2.0.0",
    "angularfire2": "^5.0.0-rc.11",
    "core-js": "^2.5.4",
    "firebase": "^5.1.0",
    "hammerjs": "^2.0.8",
    "jquery": "^3.3.1",
    "ng-scrollreveal": "^2.2.0",
    "ng2-scroll-to-el": "^1.2.1",
    "ngx-facebook": "^2.4.0",
    "ngx-infinite-scroll": "^6.0.1",
    "ngx-sharebuttons": "^4.1.4",
    "rxjs": "^6.2.1",
    "rxjs-compat": "^6.2.1",
    "time-ago-pipe": "^1.3.2",
    "youtube-search": "^1.1.1",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.8",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "@types/scrollreveal": "0.0.3",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }
}

Does somebody know what is going wrong? None of the modules mentioned above exist in my node_modules file (I can install some of them, but 'crypto', for example, is built in now). How can I get those folders? It's driving me nuts.

有人知道出了什么问题吗?上面提到的模块都不存在于我的 node_modules 文件中(我可以安装其中的一些,但是现在内置了“crypto”)。我怎样才能得到这些文件夹?它让我发疯。

回答by snorberhuis

This is caused by dependencies importing node modules that are not available in the browser. Add to your package.json the modules that give errors:

这是由导入浏览器中不可用的节点模块的依赖项引起的。将出现错误的模块添加到您的 package.json 中:

"browser": {
    "http": false,
    "https":false,
    "net": false,
    "path": false,
    "stream": false,
    "tls": false
}

回答by Azkar Moulana

You are using the latest version of Angular CLI. Some npm packages are no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in.

您正在使用最新版本的 Angular CLI。不再支持某些 npm 包。它现在是一个内置的 Node 模块。如果您依赖加密,则应切换到内置加密。

To fix your issue with crypto, stream libs, go to,

要解决您的加密问题,请访问流库,

node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js

node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js

file and do the following change,

文件并进行以下更改,

`node: {crypto: true, stream: true}`

回答by MAlinda

You can user patch file solve this problem.

您可以使用补丁文件来解决这个问题。

patch.js

补丁.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

包.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 serveonly for the first time.

确保ng serve仅在第一次之前“构建”代码。

回答by Jebe

Use ngx-build-plus package - https://github.com/manfredsteyer/ngx-build-plus- and provide the additional configuration. This way you don't have to manually update the node modules files everytime you do an install.

使用 ngx-build-plus 包 - https://github.com/manfredsteyer/ngx-build-plus- 并提供额外的配置。这样您就不必在每次安装时手动更新节点模块文件。

回答by Sanju Varghese

You have to find the node packages added as dependencies which is causing this issue. Open package-lock.json, search for crypto can find the chain of dependencies leading to the package name included in dependencies section of package.json. In my case, most of these libraries were brought in with npm package 'request'.

您必须找到作为依赖项添加的节点包,这导致了此问题。打开package-lock.json,搜索crypto可以找到导致package.json的dependencies部分包含的包名的依赖链。就我而言,这些库中的大多数都是通过 npm 包“请求”引入的。

回答by Harini P

The current version of Angular-cli doesn't install some packages like zlib which the older versions did. You may have to install some packages manually to resolve these errors.

当前版本的 Angular-cli 没有安装一些像 zlib 这样旧版本安装的软件包。您可能需要手动安装一些软件包才能解决这些错误。

回答by pipedreambomb

I just ran into this issue as someone new to Angular, and all these other answers are actually giving you workarounds for something that shouldn't be worked around (in most cases). Actually, you need to step back and consider that you are doing something the framework does not want you to do.

我刚刚作为 Angular 的新手遇到了这个问题,所有这些其他答案实际上都为您提供了一些不应该解决的方法(在大多数情况下)。实际上,您需要退后一步并考虑您正在做框架不希望您做的事情。

In my case, what happened was, I added a dependency on a library for accessing an external API service and I tried to import it in an Angular "service". I'm still new to Angular and am coming from a C# WCF background, so in my head, a service is a server-side process. However, nothing in Angular is server-side! It is a strictly client-side framework that runs in the browser.

就我而言,发生的情况是,我添加了对用于访问外部 API 服务的库的依赖,并尝试将其导入到 Angular“服务”中。我还是 Angular 的新手,并且来自 C# WCF 背景,所以在我看来,服务是一个服务器端进程。然而,Angular 中没有任何东西是服务器端的!它是一个严格的客户端框架,在浏览器中运行。

The way to fix this for me was to realise that my Angular service needs to strictly communicate with my own back-end, not outside websites, which meant an API that I was going to need to write myself, separately. In my case, I'm going with a MEAN stack, so this means creating an Express.js API that will communicate with the external API for me on the back-end. This has added advantages, such as being able to cache session data and other data from the external API in a Mongo database, instead of needing a fresh client-side API session every time, which would quickly exceed the daily sessions this site allows of 7500 a day, assuming I got a lot of users.

为我解决这个问题的方法是意识到我的 Angular 服务需要与我自己的后端而不是外部网站进行严格的通信,这意味着我需要单独编写一个 API。就我而言,我将使用 MEAN 堆栈,因此这意味着创建一个 Express.js API,它将在后端为我与外部 API 进行通信。这增加了一些优势,例如能够在 Mongo 数据库中缓存来自外部 API 的会话数据和其他数据,而不是每次都需要一个新的客户端 API 会话,这将很快超过该站点允许的每日会话数 7500一天,假设我有很多用户。

TL;DR the fix is to remove any imports to NPM packages not intended for front-end work, that require such packages like https, cryptoand fs. The fsis a particular red flag. I think this implies 'file system', which your front-end certainly shouldn't have access to directly.

TL;DR 修复是删除任何不用于前端工作的 NPM 包的导入,这些包需要像httpscryptofs. 这fs是一个特殊的红旗。我认为这意味着“文件系统”,您的前端当然不应该直接访问它。