Error: Cannot find module 'jose' in node js

7.3k views Asked by At

I installed the module. Only this module is not working other modules are working fine. When I trying to run it it's throwing following error

Error: Cannot find module 'jose'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (<my_folder>\test.js:1:100)     
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)   
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)

What I tried:

Almost everything from this stackoverflow question

  1. Removed node modules folder and installed again

  2. Cleared cache and installed again

  3. Installed npm install @types/node --save-dev

my package.json

{
  "name": "test-api",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "func start",
    "test": "echo \"No tests yet...\""
  },
  "dependencies": {
    "axios": "^0.21.0",
    "axios-retry": "^3.1.9",
    "fs-extra": "^9.0.1",
    "qs": "^6.9.4"
  },
  "devDependencies": {
    "@types/node": "^14.14.10",
    "jose": "^3.1.3"
  }
}
5

There are 5 answers

0
AudioBubble On BEST ANSWER

The jose package exports individual ESM modules, as in, there's no root jose module in version 3.x that you're installing. You can read how to require/import each individual module in the project's documentation

4
farvilain On

Maybe you should use npm install --save-dev jose to install jose :D

0
Quinten C On

If you are using Nuxt you can resolve this error by adding the types into the build transpile option in nuxt.config.js like so:

 build: {
        transpile: ['@jose/dist/types']
    },

After this I am able to import everything from Jose like so:

import {jwtVerify, importSPKI} from "jose";

Doing it like this gives you full type descriptions and everything!

0
SummmerFort On

Update your NodeJS runtime. Fixed the issue for me.

1
Erick Gonzalez On

I solved this problem on react as follows

import { jwtVerify } from 'jose/dist/browser/jwt/verify'