jose: Module not found: Can't resolve 'jose/jwt/sign'

Describe the bug

To Reproduce

  1. npm install jose
  2. Try to import like so: import { SignJWT } from “jose/jwt/sign”;
  3. Typescript compiler errors: Module not found: Can’t resolve ‘jose/jwt/sign’

Code to reproduce the behaviour: https://stackblitz.com/edit/react-ts-kkyw3y

Expected behaviour Should import correctly with no errors

Environment:

  • jose version: v3.11.6
  • affected runtime is: Node.js 14.15.0
  • other relevant library versions: typescript@4.2.4

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 24 (11 by maintainers)

Most upvoted comments

I have this error in a standard create-react-app typescript setup. I think this should be re-opened and investigated. if we need to take care of something maybe the fix is just adding some guidance on the read.me file.

@dalegacusan you’re barking up the wrong tree - this is to be solved in your tooling, not here.

Same issue here when using the package with a vue-cli setup:

 ERROR  Failed to compile with 1 error                                                                                                                                                                                          11:30:12

This dependency was not found:

* jose/jwt/verify in ./src/service/license.ts

To install it, you can run: npm install --save jose/jwt/verify
No type errors found
Version: typescript 4.3.2

Using node v14.17.0

In my experience, this can be caused by a missing “main”

There’s no “main” entry because there’s no “main” package or export. It’s just submodules, using a node loader feature present in the supported runtimes. Tooling is slowly catching up, but is just not quite there yet.

so I don’t see how the import would ever work as documented

Well, clearly they do - there are plenty of consumers of this package major already. Plus, see the proof of working code i’ve posted above.

Am I missing something here?

https://nodejs.org/api/packages.html#packages_subpath_exports https://nodejs.org/api/packages.html#packages_conditional_exports

I’m solving this problem by importing the relevant files directly. I put this into an intermediate file jose.ts and can then import like I expect to from that. Eg. like this:

import type * as T_SignJWT from 'jose/dist/types/jwt/sign'
export const { default: SignJWT } = require('jose/dist/node/cjs/jwt/sign') as typeof T_SignJWT

import type * as T_jwtVerify from 'jose/dist/types/jwt/verify'
export const { default: jwtVerify } = require('jose/dist/node/cjs/jwt/verify') as typeof T_jwtVerify

And then I can use it like this:

import { SignJWT } from './jose'

Yes I know I’m relying on internals here, but if it breaks, I can quickly fix it by editing one file on my end.