74

我在运行时收到以下错误列表ng serve

我的包JSON如下:

{   "name": "ProName",   "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-devkit/build-angular": "~0.12.0",
    "@angular/animations": "5.2.10",
    "@angular/common": "5.2.10",
    "@angular/compiler": "5.2.10",
    "@angular/compiler-cli": "5.2.10",
    "@angular/core": "5.2.10",
    "@angular/forms": "5.2.10",
    "@angular/platform-browser": "5.2.10",
    "@angular/platform-browser-dynamic": "5.2.10",
    "@angular/router": "5.2.10",
    "@types/dotenv": "^4.0.3",
    "@types/errorhandler": "0.0.32",
    "@types/express": "^4.16.0",
    "@types/node": "^10.5.1",
    "apostille-library": "^7.1.0",
    "core-js": "^2.5.4",
    "dotenv": "^6.0.0",
    "errorhandler": "^1.5.0",
    "express": "^4.16.0",
    "nem2-sdk": "^0.9.7",
    "rxjs": "~6.3.3",
    "stream": "0.0.2",
    "tslib": "^1.9.0",
    "typescript": "^2.9.2",
    "zone.js": "~0.8.26"   } }

我得到的错误:

./node_modules/aws-sign2/index.js 中的错误 找不到模块:错误:无法解析 '/Users/MYPC/Documents/Myproj/ProName/node_modules/aws-sign2' 中的 'crypto' ./node_modules 中的错误/aws4/aws4.js 未找到模块:错误:无法解析“/Users/MYPC/Documents/Myproj/ProName/node_modules/aws4”中的“加密” ./node_modules/ecc-jsbn/index.js 模块中的错误未找到:错误:无法解析 '/Users/MYPC/Documents/Myproj/ProName/node_modules/ecc-jsbn' 中的 'crypto' ./node_modules/http-signature/lib/verify.js 中的错误 找不到模块:错误:无法解析 '/Users/MYPC/Documents/Myproj/ProName/node_modules/http-signature/lib' 中的 'crypto' ./node_modules/http-signature/lib/signer.js 中的错误 找不到模块:错误: 无法解析 ' 中的 'crypto'/Users/MYPC/Documents/Myproj/ProName/node_modules/http-signature/lib' ./node_modules/nem-sdk/build/external/nacl-fast.js 中的错误模块未找到:错误:无法解析'crypto ' 在 '/Users/MYPC/Documents/Myproj/ProName/node_modules/nem-sdk/build/external' 中的错误 ./node_modules/nem-sdk/node_modules/aws-sign2/index.js

4

16 回答 16

118

我最近在我正在试验的一个小项目中尝试使用另一个库( tiff.js )时遇到了类似的问题。

我解决这个问题的方法是将以下内容添加到我的package.json文件中,就在该devDependencies部分之后。

"devDependencies": {
    ...
},
"browser": {
    "crypto": false
}

尝试在应用程序中使用库时,这似乎没有任何不利影响。

于 2019-01-12T18:05:58.447 回答
44

在该项目下的tsconfig.json文件中添加此设置可解决此警告

"compilerOptions": {
"baseUrl": "./",
"paths": {
  "crypto": [
    "node_modules/crypto-js"
  ]
}
于 2020-07-07T20:40:16.587 回答
36

我喜欢R. Richards的回答,但我认为提供更多信息会很有用。

这是 Angular 的一个已知问题,Angular CLI 开发团队似乎认为这是一个特性而不是错误。我以及此问题线程中的其他开发人员不同意。该线程的贡献者提供了几个解决方法修复,但我的项目在我实施 R. Richards 的解决方案之前没有成功编译。不过,我没有恢复以前的更改,因此tacnomanGrandSchtroumpf的修复可能对其他人有用。

有些人,比如这里的clovis1122和那个问题线程中的其他人,质疑为什么 Web 应用程序需要访问这些库以及为什么不能在服务器端完成必要的任务。我不能代表所有人,但我的用例是,在验证用户帐户时,Strapi 会使用必须由客户端解码的 JSON Web Token 字符串进行响应。由于必要的库依赖于cryptoand stream,除非这些依赖项可用,否则您将无法提取 JWT 过期时间。

如果有人无法从 R. Richards 的答案中推断出来,您必须将任何出现在“无法解决 x”错误中的依赖项设置为 false。例如,我的 package.json 的关键部分是:

    "browser": {
        "crypto": false,
        "stream": false
    }
于 2019-03-31T08:29:13.603 回答
25

我想我会扩展 Tarique Ahmed 在他的回答中所写的内容。

我使用的 npm 模块在代码中有以下行:

const crypto = require('crypto');

我无法添加:

"browser": {
  "crypto": false
}

到 package.json 因为crypto包必须是构建的一部分。

事实证明,在编译过程中,Angular 似乎已经决定安装crypto-browserify包而不是crypto.

将以下内容添加到tsconfig.json文件中会指示构建在crypto-browserify每次crypto需要时使用该库。如您所见,我对stream包裹有同样的问题。

"paths": {
  "crypto": [
    "node_modules/crypto-browserify"
  ],
  "stream": [
    "node_modules/stream-browserify"
  ]
}
于 2020-09-10T12:19:44.087 回答
18

在 Angular 11 和 crypto-js 4 遇到同样的问题(并在 tsconfig.json 中手动设置路径)后,我发现将 crypto-js 回滚到版本 3.1.9-1 解决了这个问题。似乎版本 4 中的更改导致了该问题。

npm install crypto-js@3.1.9-1

在回购问题中解释:

GitHub问题

于 2020-12-30T11:57:38.013 回答
13

如果您升级到 Webpack 5,则需要将其添加到您的webpack 配置文件中

resolve: {
    fallback: { crypto: false },
},
于 2021-04-13T14:12:15.740 回答
2

我已使用以下步骤解决了我的问题:

将以下内容添加到tsconfig.json以解决加密警告:

"paths": {
      "crypto": [
        "node_modules/crypto-js"
      ]
    },

并将下面添加到angular.json

"options": {
"allowedCommonJsDependencies": [
              "crypto-js"
            ],
...
}
于 2021-09-03T06:58:31.627 回答
2

aws-sign2是一个 NodeJS 包(并且crypto是一个 NodeJS 模块),但看起来您正在处理一个 Web 应用程序。crypto该模块在该环境中不可用是有道理的。

是否有可能在服务器端完成您需要做的事情?否则,您可能需要寻找另一个包裹。

于 2019-01-13T06:09:50.343 回答
1

对于 Laravel Inertia JS 项目,我的解决方案是:

1-将依赖项添加到 package.json

   "dependencies": {
        "crypto-browserify": "3.12.0",
        "crypto-random-string": "^3.3.0",
        "stream": "^0.0.2"
    }

2-在 webpack.config.js 中:

const path = require('path');

module.exports = {
    resolve: {
        alias: {
            '@': path.resolve('resources/js'),
        },
        fallback: {
            crypto: require.resolve('crypto-browserify'),
            stream: require.resolve('stream'),
        },
    },
};

3-安装、构建和运行:

npm install && npm run watch
于 2021-07-15T21:21:54.163 回答
0

使用直接导入可能不适用于 ES6 环境。

这可能会对您有所帮助。

$ npm i crypto-js@latest// 使用最新版本 4

import AES from 'crypto-js/aes';
import Utf8 from 'crypto-js/enc-utf8';
import { secretKey } from './environments/environment';

/** Encryption */
const data = {key: 'Test Value'};
const ciphertext = AES.encrypt(JSON.stringify(data), secretKey).toString();
console.log('Encrypted Data', ciphertext);

/** Decryption */
const bytes = AES.decrypt(ciphertext, secretKey);
const decryptedData = JSON.parse(bytes.toString(Utf8));
console.log('Decrypted Data', decryptedData);

https://github.com/brix/crypto-js/issues/168#issuecomment-785617218

于 2021-02-25T05:18:23.460 回答
0

在数组中添加带有文字“crypto-js”的 allowedCommonJsDependencies 选项,这在文件 angular.json 中:

"architect": 
        "build": {              
          "options": {               
            "allowedCommonJsDependencies": [
              "crypto-js"
            ]
          },
         }
 }

这将禁用在 Angular 11 中测试的所有警告。

于 2021-03-31T16:08:23.467 回答
0

经过深入研究,我发现解决方案非常简单:将 import * as CryptoJS from 'crypto-js' 替换为 CryptoJS;声明 var CryptoJS;

于 2020-08-04T10:02:19.420 回答
0

我的问题是我试图使用相同的代码构建到节点和 web,但是在导入 WebSocket 依赖项时无法构建到 web,在我的情况下是ws

所以解决方案是使用包装器:

安装一个包装器,我将使用isomorphic-ws因为是为ws制作的

npm i --save isomorphic-ws

消除const WebSocket = require('ws')

用。。。来代替:

const WebSocket = require('isomorphic-ws')
于 2021-08-28T23:53:20.223 回答
0

我最终进入

node_modules/react-scripts/config/webpack.config.js

并添加:

fallback: {
        // Here paste
        crypto: require.resolve("crypto-browserify"),
        https: require.resolve("https-browserify"),
        http: require.resolve("stream-http"),
        url : require.resolve("url")
      }

现在我的反应应用程序构建错误但没有依赖问题。当我建立它时,我会更新它。

于 2022-01-12T22:36:46.100 回答
0

添加

npm install crypto-js

或根据您的项目需要添加特定版本

npm install crypto-js@4.0.0

此外,在窗口“以管理员身份运行”或在 Linux 中使用 sudo 运行上述命令

于 2022-02-27T16:02:08.017 回答
-2

我在使用 create-react-app(facebook) 的 ReactJS 中遇到了这个问题

解决方案:

  1. 首先安装必要的包“crypto-browserify”

  2. 用 create-react-app 修改 reactjs 中的 webpack.config.js 这个文件在里面:

node_modules/react-scripts/config/webpack.config.js

  • 搜索 module.exports 并在此函数内部有一个返回:
module.exports = function (webpackEnv) {
  ...
  return {
   ...
    resolve: {
      ...
      fallback: {
        // Here paste
        crypto: require.resolve("crypto-browserify"),

      }
    }
  }
}

注意:您是否可能需要其他包如何“流浏览”步骤是相同的​​。此解决方案有效,但是当 webpack 项目启动时,它会显示警告

Pd:我不是以英语为母语的人,但我希望能理解我。

于 2021-12-19T16:58:45.090 回答