我将在我的angular6项目npm i jsonwebtoken
中安装jsonwebtoken模块。安装了jsonwebtoken模块Dependencies jwa,但是那个index.js文件crypto需要错误无法解析'crypto',但是我已经安装了crypto模块。
请帮助并解决问题。
我的错误:
ERROR in ./node_modules/jwa/index.js
Module not found: Error: Can't resolve 'crypto' in '/opt/lampp/htdocs/angular-testing-app/node_modules/jwa'
ERROR in ./node_modules/jws/lib/sign-stream.js
Module not found: Error: Can't resolve 'stream' in '/opt/lampp/htdocs/angular-testing-app/node_modules/jws/lib'
ERROR in ./node_modules/jws/lib/verify-stream.js
Module not found: Error: Can't resolve 'stream' in '/opt/lampp/htdocs/angular-testing-app/node_modules/jws/lib'
ERROR in ./node_modules/jws/lib/data-stream.js
Module not found: Error: Can't resolve 'stream' in '/opt/lampp/htdocs/angular-testing-app/node_modules/jws/lib'
答案 0 :(得分:6)
我遇到了与您相同的问题,并且解决了。 您可以按照以下步骤操作:
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": {
"postinstall": "node patch.js",
...
}
}
参考:https://gist.github.com/niespodd/1fa82da6f8c901d1c33d2fcbb762947d
答案 1 :(得分:1)
但是我没有手动安装,而是手动编辑了“ node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js”并更改了行在该正则表达式中:
// old:
node: false,
// new:
node: { crypto: true, stream: true },
答案 2 :(得分:0)
您可以使用此脚本修改browser.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);
}
//necesario para que jsforce y algunas bibliotecas que aun usan el global de nodejs
let result = data.replace(/node: false/g,
`node:
{
crypto: true,
stream: true,
fs: 'empty',
global: true,
tls: 'empty',
net: 'empty',
process: true,
module: false,
clearImmediate: false,
setImmediate: false
}`);
fs.writeFile(f, result, 'utf8', function (err) {
if (err) return console.error(err);
});
});