I'm using the mswjs library for mocking APIs in a Nuxt 3 (3.0.0-rc.11) project; Everything is working fine until I try to import the 'jsonwebtoken' library to generate a JWT access token to mock the login API call.
Here is the login request handler:
//file path: @/mocks/auth.js
import {rest} from 'msw'
import { addBaseUrl, makeResponse } from './helpers'
import jwt from 'jsonwebtoken'
export default [
rest.post(addBaseUrl('/api/auth/login/'), (req, res, ctx) => {
const json = {
access: jwt.sign(
{test: 'test'},
'secret',
{expiresIn: 600} )
}
return makeResponse({res, ctx, status: 200, delay: 1000, json})
}),
]
The problem is that when I try to import jwt
, it throws this error and everything stops working:
util.js:109 Uncaught TypeError: Cannot read properties of undefined (reading 'NODE_DEBUG')
at node_modules/util/util.js (util.js:109:17)
at __require (chunk-7NEK6ARH.js?v=bb8c835c:9:50)
at node_modules/jws/lib/data-stream.js (data-stream.js:4:12)
at __require (chunk-7NEK6ARH.js?v=bb8c835c:9:50)
at node_modules/jws/lib/sign-stream.js (sign-stream.js:3:18)
at __require (chunk-7NEK6ARH.js?v=bb8c835c:9:50)
at node_modules/jws/index.js (index.js:2:18)
at __require (chunk-7NEK6ARH.js?v=bb8c835c:9:50)
at node_modules/jsonwebtoken/decode.js (decode.js:1:11)
at __require (chunk-7NEK6ARH.js?v=bb8c835c:9:50)
The problem is caused by jws library which is a dependency of jsonwebtoken
library. I have no idea about how to fix this issue, and I would really appreciate any help.
Here is my package.json file:
{
"private": true,
"scripts": {
"start": "nuxt start",
"build": "nuxt build",
"dev": "nuxt dev --https --ssl-cert localhost.pem --ssl-key localhost-key.pem",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"@iconify/vue": "^4.0.0",
"autoprefixer": "^10.4.8",
"msw": "^0.47.3",
"nuxt": "3.0.0-rc.11",
"postcss": "^8.4.16",
"tailwindcss": "^3.1.8"
},
"dependencies": {
"@vue-leaflet/vue-leaflet": "^0.6.1",
"@vuelidate/core": "^2.0.0-alpha.44",
"@vuelidate/validators": "^2.0.0-alpha.31",
"dayjs": "^1.11.5",
"jsonwebtoken": "^8.5.1",
"jwt-decode": "^3.1.2",
"leaflet": "^1.9.1",
"vue-google-charts": "^1.1.0",
"vue3-carousel": "^0.1.46"
},
"packageManager": "yarn@3.2.3",
"msw": {
"workerDirectory": "public"
}
}