fastify / fastify-passport

Use passport strategies for authentication within a fastify application
MIT License
260 stars 47 forks source link

Cannot find module '@fastify/session' or its corresponding type declarations #1217

Open RedBoardDev opened 1 month ago

RedBoardDev commented 1 month ago

Prerequisites

Fastify version

5.0.0

Plugin version

3.0.1

Node.js version

20.17.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

UX425QA.306

Description

I have a typescript compilation error in the typing module. When I run ‘tsc --noEmit’, I get this error:


node_modules/@fastify/passport/dist/session-managers/SecureSessionManager.d.ts:4:38 - error TS2307: Cannot find module '@fastify/session' or its corresponding type declarations.

4 import { FastifySessionObject } from '@fastify/session';
                                       ~~~~~~~~~~~~~~~~~~

Found 1 error in node_modules/@fastify/passport/dist/session-managers/SecureSessionManager.d.ts:4

And all my dependencies from package.json:

    "@fastify/cors": "^10.0.1",
    "@fastify/helmet": "^12.0.1",
    "@fastify/passport": "^3.0.1",
    "@fastify/rate-limit": "^10.1.1",
    "@fastify/secure-session": "^8.1.0",
    "@fastify/swagger": "^9.1.0",
    "@fastify/swagger-ui": "^5.0.1",
    "@prisma/client": "^5.20.0",
    "ajv": "^8.17.1",
    "ajv-formats": "^3.0.1",
    "bcrypt": "^5.1.1",
    "dotenv": "^16.4.5",
    "fastify": "^5.0.0",
    "handlebars": "^4.7.8",
    "json-schema-to-ts": "^3.1.1",
    "jsonwebtoken": "^9.0.2",
    "nodemailer": "^6.9.15",
    "passport-local": "^1.0.0"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.2",
    "@types/cors": "^2.8.17",
    "@types/jsonwebtoken": "^9.0.7",
    "@types/node": "^22.5.5",
    "@types/nodemailer": "^6.4.16",
    "@types/passport": "^1.0.16",
    "@types/passport-local": "^1.0.38",
    "eslint-plugin-import": "^2.31.0",
    "eslint-plugin-unused-imports": "^4.1.4",
    "husky": "^9.1.6",
    "prettier": "^3.3.3",
    "prisma": "^5.20.0",
    "ts-node": "^10.9.2",
    "ts-node-dev": "^2.0.0",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.6.0",
    "typescript-eslint": "^8.8.0"
  }

tsconfig.json:

{
  "extends": "./tsconfig.base.json",
  "ts-node": {
    "transpileOnly": true,
    "require": ["tsconfig-paths/register"]
  },
  "include": ["app/**/*.ts"],
  "exclude": ["dist", "node_modules"]
}

tsconfig.base.json:

{
  "compilerOptions": {
    "lib": ["ESNext"],
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "removeComments": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "sourceMap": true,
    "target": "ES2020",
    "esModuleInterop": true,
    "outDir": "./dist",
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "types": ["node"],
    "typeRoots": ["./node_modules/@types", "./app/src/entities"],
    "baseUrl": "./app/src",
    "paths": {
      "@fonts": ["assets/fonts/*"],
      "@icons": ["assets/icons/*"],
      "@images": ["assets/images/*"],
      "@config/*": ["config/*"],
      "@entities/*": ["entities/*"],
      "@enums/*": ["enums/*"],
      "@libs/*": ["libs/*"],
      "@middlewares/*": ["middlewares/*"],
      "@repositories/*": ["repositories/*"],
      "@routes/*": ["routes/*"],
      "@services/*": ["services/*"],
      "@templates": ["templates/*"],
      "@utils": ["libs/utils/index.ts"]
    },
    "strictNullChecks": true
  }
}

Don't hesitate if you see any inconsistencies in these files too !

Link to code that reproduces the bug

No response

Expected Behavior

No response

mcollina commented 1 month ago

I think this was introduced in https://github.com/fastify/fastify-passport/pull/1198. A workaround for you could be to use the skipLibCheck option.

cc @robwalkerco @simoneb - I have absolutely no idea how to type this.

RedBoardDev commented 1 month ago

Yeah, but I want to avoid skipLibCheck to have a nice CI ! But for now I'm using it yes

robwalkerco commented 1 month ago

The Session and Secure-session packages are used for some of the types, but they are dev dependencies https://github.com/fastify/fastify-passport/blob/223d097ce47b62210054714959c7007a0b518471/package.json#L48-L49 This lets the types work great when developing, but causes the error when checking the types when the dev dependencies are not installed, such as in a production CI environment.

I'll have to do some research tomorrow on the best way to handle this scenario, as I guess the preference would be to avoid making them dependencies.

robwalkerco commented 1 month ago

I've tried to find a nice way to make this work well, and have yet to find a great solution.

I have managed to make it work with some type changes in @fastify/session and @fastify/secure-session, but that breaks the existing SessionData typing :(

I will continue to investigate when I next get some time

eiskalteschatten commented 3 weeks ago

I'm also having this issue which is preventing me from updating...