我有一个使用 angular-cli 的 angular2 应用程序用于脚手架和其他任务,但现在我不能使用 jsonwebtoken
在我的 angular2 应用程序上。
我添加了依赖
yarn add --save jsonwebtoken
我实际上可以在 node_modules
看到这个库,我做了
从'jsonwebtoken'导入{jwt}
在我的 TypeScript 代码中,IDE 没有给我任何警告,因此它可以正确找到依赖项。
但是当我执行 npm start
或 yarn start
时,我有以下错误消息
ERROR in ./~/isemail/lib/index.js
Module not found: Error: Can't resolve 'dns' in '/home/$MY_USER_NAME_PLACE_HOLDER/dir/to/my/app/node_modules/isemail/lib'
@ ./~/isemail/lib/index.js 5:12-26
@ ./~/joi/lib/string.js
@ ./~/joi/lib/index.js
@ ./~/jsonwebtoken/sign.js
@ ./~/jsonwebtoken/index.js
@ ./src/app/_auth/authentication.service.ts
@ ./src/app/app.component.ts
@ ./src/app/index.ts
@ ./src/main.ts
@ multi main
ERROR in [default] /home/$MY_USER_NAME_PLACE_HOLDER/dir/to/my/appsrc/app/_auth/authentication.service.ts:41:24
Property 'validate' does not exist on type 'typeof "/home/$MY_USER_NAME_PLACE_HOLDER/dir/to/my/app/node_modules/@types/jsonwebtoken/index"'.
当我在互联网上搜索错误时,我跟踪了一系列对话和相关项目。 我发现:
有类似的问题,但没有detailed它没有回答。
react-validation-mixin中的一些相关错误, isemail和 joi带我到这个 one ,当他们描述从节点导入依赖于
dns
、net
或其他一些 native 模块的库时出现类似问题,这让我觉得在使用这种类型时存在一些问题专门使用webpack
的库(我没有专业知识可以确定这一点,如果我错了请更正),解决方法是将以下行添加到webpack.config
node: { net: 'empty', tls: 'empty', dns: 'empty' }
但我的问题是,angular-cli 有 no way to override webpack config并且无意改变这一点。
所以我不知道在这里做什么,你知道如何在我的 angular-cli 应用程序中使用 jsonwebtoken 吗?
最佳答案
我在尝试使用 jsonwebtoken 客户端时遇到了同样的问题(jsonwebtoken 在我的 Express 应用程序中在服务器端工作得很好)。
简短的回答是可能无法使用 https://github.com/Hendrixer/jwt-decode jsonwebtoke 在你的 Angular 应用程序中。如果您只想解码 JWT 的客户端以执行验证等操作,那么该库有点重,而且该模块实际上是为 node.js 中的服务器端使用而设计的。
但是,有许多可用的替代方案可以在 Angular 2 中的客户端工作(包括在 angular-cli、webpack 中)。 Jwt-decode例如,将解码 JWT。
但是,我发现最好的选择是重构我的代码以使用 Angular2-JWT的 AuthHttp 类负责为您在 http header 中传递 token 。如果 token 不存在或未通过验证,它也会抛出错误,您可以随意处理。
关于angular - 在 angular-cli 应用程序中使用 jsonwebtoken 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41212499/