I'm making a nodeJS web app and I'm using JWT for authentication. All my work is in ES6 modules and I wanted to import JWT in the same way, but apparently it isn't yet supported by the package. I can't use the older require() format as it throws an error since I've set it to modules in my package.json. Is there a way around this or do I have to find another library altogether?
Edit: I have fixed the issue with destructuring but it's still not working. Apparently it can't find the module at all. I made sure the package is in fact installed and updated, still not working
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'jsonwebtoken' imported from /path/to/file.js
import jwt from ' jsonwebtoken'
const { sign } = jwt
class sampleClass {
static func(user) {
return sign(
{
_id: user._id,
name: user.name,
},
'sample key',
{
expiresIn: '7d',
},
)
}
}
I'm making a nodeJS web app and I'm using JWT for authentication. All my work is in ES6 modules and I wanted to import JWT in the same way, but apparently it isn't yet supported by the package. I can't use the older require() format as it throws an error since I've set it to modules in my package.json. Is there a way around this or do I have to find another library altogether?
Edit: I have fixed the issue with destructuring but it's still not working. Apparently it can't find the module at all. I made sure the package is in fact installed and updated, still not working
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'jsonwebtoken' imported from /path/to/file.js
import jwt from ' jsonwebtoken'
const { sign } = jwt
class sampleClass {
static func(user) {
return sign(
{
_id: user._id,
name: user.name,
},
'sample key',
{
expiresIn: '7d',
},
)
}
}
Share
Improve this question
edited Oct 9, 2020 at 7:49
Unknown6003
asked Oct 9, 2020 at 7:26
Unknown6003Unknown6003
1731 gold badge1 silver badge9 bronze badges
5
-
did you try to execute your file with the experimental flag
node --experimental-modules app.js
– Ilijanovic Commented Oct 9, 2020 at 7:28 - No way you cant use es6 import, you doing it wrong. – Talg123 Commented Oct 9, 2020 at 7:29
- Yes, ES6 import functionality is working with all other packages, the issue is with JWT as it does not support ES6 imports, the issue has been raised on github but no solution other than modifying the package manually, which leads to a massive package size. – Unknown6003 Commented Oct 9, 2020 at 7:30
- 1 Are you kidding me? I just installed jwt and everything is fine. Im not sure what did you try to do, post you code. – Talg123 Commented Oct 9, 2020 at 7:32
- I have updated and getting a new error apparently. – Unknown6003 Commented Oct 9, 2020 at 7:52
6 Answers
Reset to default 11Your gonna need to import it and then assign it like this
import jwt from 'jsonwebtoken';
const { sign, verify } = jwt;
const token = sign({"d":"dd"}, "secret", {expiresIn: 300})
console.log(token);
const verifycode = verify(token, "secret");
console.log(verifycode);
If you are using jwt v8, just import the jsonwebtoken this way:
import * as jwt from 'jsonwebtoken'
or
import { sign, decode, verify } from 'jsonwebtoken'
Could you try something:
- Create a folder
- Do
npm init
- Create a file
app.js
- install json web token
npm i jsonwebtoken
- Go to
package.json
and add"type": "module"
- write in your
app.js
this here:import jwt from "jsonwebtoken"
- Execute it:
node --experimental-modules app.js
Tell me then if you get an error
You have an extra space in import jwt from ' jsonwebtoken'
Should just be import jwt from 'jsonwebtoken'
I just tested it and it works fine on my puter
I am using typescript to write my node server and jwt implementation.
This is the suggestion on my terminal:
Try npm i --save-dev @types/jsonwebtoken
if it exists or add a new declaration (.d.ts) file containing `declare module 'jsonwebtoken';
I just typed: npm i --save-dev @types/jsonwebtoken
Then in my file I added imports: import jwt from "jsonwebtoken"
Hope that helps.
import Jwt from "jsonwebtoken";
const jwt = Jwt;
I can use it like this