I’m building my first API web project and I have encountered an error:
return expressJwt({
^
TypeError: expressJwt is not a function
This is my code :
const expressJwt = require('express-jwt');
function authJwt() {
const secret = process.env.secret;
return expressJwt({
secret,
algorithms: ['HS256'],
})
3
Answers
you are not using correctly
espress-jwt
when you doYou are importing the all library, however, you need specifically
expressJwt
method, so you can do something likeOr even better, as explained in the official documentation
Since you are importing the whole module, you have to specify the method to use. For example:
When you import a module in Node.js you need to know what is being exported by the module.
You are using the express-jwt module with CommonJS syntax which exports an object with a key of
expressjwt
and the value of which is a function namedexpressjwt
. It looks like this:If you want to use the
expressjwt
function then you can use object destructing to import it like so:If you want to rename the
expressjwt
function when you import it you can. The official docs give an example of renaming it tojwt
like this:To get your code working you can rename
expressjwt
toexpressJwt
very easily like this: