When I imported 'jsonwebtoken' into angular2 web app, encountered error as below:
// example
import * as jwt from 'jsonwebtoken';
let token = jwt.sign({
username: username,
role: role,
exp: Math.floor(Date.now() / 1000) + (60 * 20)
}, 'xxx');
// error
ERROR in ./~/isemail/lib/isemail.js
Module not found: Error: Can't resolve 'dns' in '/Users/aaa.../myApp/node_modules/isemail/lib'
@ ./~/isemail/lib/isemail.js 45:10-24
@ ./~/isemail/index.js
@ ./~/joi/lib/string.js
@ ./~/joi/lib/index.js
@ ./~/jsonwebtoken/sign.js
@ ./~/jsonwebtoken/index.js
@ ./src/app/authentication/fake/fake-backend.ts
@ ./src/app/authentication/fake/index.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi main
We use joi for the validations, there are some issues using it in the browser instead of in NodeJS.
The issue you describe is in their repo too https://github.com/hapijs/joi/issues/706
Just for curiosity, may I ask what is the secret you want to use to sign in a browser? the user password? And what is the purpose of signing in browser for you?
@ziluvatar I have the same issue..
I'm using it server side but I also need it browser side to implement a mock for development purpose.. But I know browser side cryptography is a bad idea..
PS: Issue related to https://github.com/hapijs/joi/issues/528
Hi @yvele, as commented here, for signing in browser you can take a look to https://github.com/kjur/jsrsasign/wiki/Tutorial-for-JWT-generation
I will close this issue, be free to re-open it if you have outstanding problems/questions.
@ziluvatar Oh thank you so much for the tutorial.. nice one 馃憤
@ziluvatar @yvele @vivian7169
I also ran into the same problem and went for a little bit of a different solution:
If youre using webpack or browserify you can shim out modules to make them point to other modules. In this case we replace calls to require('joi') using the module loader.
For a bit more details see the following repos (depending on which version of joi you're using):
This will take care of allowing you to load in the browser or in node (handy for isomorphic applications).
__NOTE:__ it may be worth bundling this for browser use and adding an entry in your package.json to toggle depending on the build. Id be willing to add a pull request for this if you're interested. On the surface it would seem like a better choice to use another library to handle decode and verification but this one is in active development and will probably outlast any of the framework specific libraries. Not to mention that it makes life easier for developers that do full stack development (like yours truly :grin:).
Taken from their docs:
If you want to use joi with Node.js and joi-browser for browser use then you can follow one of these recipies.
npm install joi-browser
npm install joi
Add the following to your app's package.json which will tell browserify to use joi-browser instead of joi when bundling for the browser.
"browser": {
"joi": "joi-browser"
},
So in your code, you just require joi and browserify will automatically switch it with joi-browser when it bundles.
var Joi = require('joi');
See examples/browserify-basic
npm install joi-browser
npm install joi
Add the following to your app's webpack.config.js to alias joi to joi-browser
resolve: {
alias: {
joi: 'joi-browser'
}
Note: if you are using webpack with a babel loader you may need to exclude joi-browser (or node_modules) from being run through babel again.
In your webpack.config.js loaders, add an exclude: [ /joi-browser/ ].
So in your code, you just require joi and webpack will automatically switch it with joi-browser when it bundles.
var Joi = require('joi');
See examples/webpack-basic
@jdrew1303 Thanks for the doc refs. The webpack one was just what I needed!
Why does jsonwebtoken need isemail? Why does isemail need dns?
@jhalbsgut No problem, glad to help. I ran into the same issues so its a pain.
@megamindbrian
They're using joi for validations, and joi is pulling these in unfortunately. 馃憥 Joi is a great lib for validations but it freaks out in the browser.
Here's the offending bit: https://github.com/auth0/node-jsonwebtoken/blob/2ec4960b8b944682aa486a4f2d054d731baf8158/sign.js#L7
It all looks like boilerplate to me ;)
I'm having the same issues - no webpack right now available.
I just want to use opentok, which uses jsonwebtoken, which uses joi, which uses isemail, which required dns a few months ago. I'm trying to overwrite the dependency right now using npm shrinkwrap: https://stackoverflow.com/questions/15806152/how-do-i-override-nested-npm-dependency-versions
Joi will fix this in their next release - commit (month ago): https://github.com/hapijs/joi/commit/6af9763e13fd0fd18afd6b720afa790e0c8467c8
shrinkwrap is not supported by yarn -> you can hardcode the module version of isemail in yarn.lock
Most helpful comment
@ziluvatar @yvele @vivian7169
I also ran into the same problem and went for a little bit of a different solution:
If youre using webpack or browserify you can shim out modules to make them point to other modules. In this case we replace calls to
require('joi')using the module loader.For a bit more details see the following repos (depending on which version of joi you're using):
This will take care of allowing you to load in the browser or in node (handy for isomorphic applications).
__NOTE:__ it may be worth bundling this for browser use and adding an entry in your package.json to toggle depending on the build. Id be willing to add a pull request for this if you're interested. On the surface it would seem like a better choice to use another library to handle decode and verification but this one is in active development and will probably outlast any of the framework specific libraries. Not to mention that it makes life easier for developers that do full stack development (like yours truly :grin:).
Taken from their docs:
Isomorphic / Universal JS - using in browser and on server (Node.js)
If you want to use
joiwith Node.js andjoi-browserfor browser use then you can follow one of these recipies.Browserify
Add the following to your app's package.json which will tell browserify to use joi-browser instead of joi when bundling for the browser.
So in your code, you just require
joiand browserify will automatically switch it with joi-browser when it bundles.See
examples/browserify-basicWebpack
Add the following to your app's webpack.config.js to alias joi to joi-browser
Note: if you are using webpack with a babel loader you may need to exclude
joi-browser(or node_modules) from being run through babel again.In your webpack.config.js loaders, add an
exclude: [ /joi-browser/ ].So in your code, you just require
joiand webpack will automatically switch it with joi-browser when it bundles.See
examples/webpack-basic