MasterKale / SimpleWebAuthn

WebAuthn, Simplified. A collection of TypeScript-first libraries for simpler WebAuthn integration. Supports modern browsers, Node, Deno, and more.
https://simplewebauthn.dev
MIT License
1.62k stars 137 forks source link

Cannot find module 'cbor-x/index-no-eval' or its corresponding type declarations. #513

Closed mat813 closed 9 months ago

mat813 commented 9 months ago

Describe the issue

With 9.0.1, I get:

$ tsc
node_modules/@simplewebauthn/server/script/deps.d.ts(2,24): error TS2307: Cannot find module 'cbor-x/index-no-eval' or its corresponding type declarations.

Reproduction Steps

  1. Update to 9.0.1 on the server
  2. Try to build
  3. Get the error

Expected behavior

Not get a build error ?

Dependencies

SimpleWebAuthn Libraries

Server and types at 9.0.1

MasterKale commented 9 months ago

Hello @mat813, thanks for the report. This is related to #511, weird that an issue like this wasn't caught locally or in CI. I'll dig into this and report back.

Can you share your project's tsconfig.json?

sualko commented 9 months ago

I have the same issue.

{
  "ts-node": {
    "files": true
  },
  "compilerOptions": {
    "module": "commonjs",
    "strict": true,
    "strictPropertyInitialization": false,
    "noImplicitOverride": true,
    "declaration": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "exclude": ["node_modules", "dist", "tmp"],
  "include": ["src", "test"]
}

Not sure if this is related, but for some reason the default export no longer works for me. Instead of

import SimpleWebAuthnServer from '@simplewebauthn/server';

I have to use

import * as SimpleWebAuthnServer from '@simplewebauthn/server';

We are running Typescript in version 5.3.3.

I removed the node_modules folder and installed all dependencies again, but the package doesn't seam to be installed.

$ npm why cbor-x/index-no-eval
npm ERR! No dependencies found matching cbor-x/index-no-eval
mat813 commented 9 months ago

Here is my tsconfig.json :

{
  "compilerOptions": {
    "target": "es2021",
    "module": "commonjs",
    "esModuleInterop": true,
    "noImplicitAny": true,
    "outDir": "./built",
    "sourceMap": true,
    "inlineSources": true,
    "sourceRoot": "/",
    "noEmitHelpers": true,
    "importHelpers": true,
    "resolveJsonModule": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "strictNullChecks": true
  },
  "types": ["jest", "jest-extended"],
  "exclude": ["node_modules"],
  "include": [
    "apps/**/*.ts",
    "graphql/**/*.ts",
    "graphql/*.d.ts",
    "mailers/**/*.ts",
    "scripts/**/*.ts",
    "ecosystem.json"
  ]
}

This still happens with @simplewebauthn/server@9.0.2 and @simplewebauthn/types@9.0.1.

spendres commented 9 months ago

Isn't @simplewebauthn/* installed in node_modules? You may need to include "node_modules/@simplewebauthn/*/.ts" since you excluded all of node_modules.

MasterKale commented 9 months ago

Good eye @spendres, I'll bet that's it. cbor-x/no-index-eval is defined in the dependency's package.json here:

https://github.com/kriszyp/cbor-x/blob/0b5e8807622619c6a7a062f7e771478ecfd52f83/package.json#L59

If TypeScript can't see any types in node_modules/ (as evident in both tsconfig.json samples pasted above) I'm not surprised it's having issues transpiling.

My suggestion to fix that tsc error is to move "node_modules" from "exclude" to "include" instead and try again.

No, wait a minute, it's not that - exclude is about what gets transpiled. I get this same issue with the example project once I install @simplewebauthn/types (oops, gotta fix that) and then run npx tsc in there.

Investigating...

MasterKale commented 9 months ago

I created https://github.com/kriszyp/cbor-x/issues/100 over in the cbor-x project because I discovered that locally duplicating their index.d.ts as index-no-eval.d.ts in my example project here's node_modules/cbor-x/ folder allowed tsc to understand types for cbor-x/index-no-eval. However I couldn't think of anything clever to do on the SimpleWebAuthn side of the house to get TypeScript to "alias" the index.d.ts type declarations for cbor-x/index-no-eval imports...

MasterKale commented 9 months ago

Okay, enough people are affected by this I think I'll have to revert #511 and figure something else out. The cbor-x project looks like it's being actively maintained, but there's evidence they may not have a lot of time to dedicate to addressing issues. I'll have to break Vercel's Edge runtime support for sake of restoring Node support, but my gut tells me there are more people in the latter group than the former.

MasterKale commented 9 months ago

I'm proposing fixing this issue by replacing use of cbor-x another library, tiny-cbor. @mat813 @sualko @mmv08 if you're feeling daring enough you can trying building and installing from the fix/513-cbor-x-type-issues branch to see if this fixes your issues. In my testing I think this is the solution, but extra confirmation couldn't hurt.

MasterKale commented 9 months ago

@mat813 @sualko @mmv08 This issue should be fixed in the newly published @simplewebauthn/server@9.0.3