简体   繁体   中英

Error: Cannot find module 'jose' in node js

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"
  }
}

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

Update your NodeJS runtime. Fixed the issue for me.

I solved this problem on react as follows

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM