iron-session icon indicating copy to clipboard operation
iron-session copied to clipboard

Type error when using a baseURL and paths in a next project

Open csprance opened this issue 3 years ago • 7 comments

I get this error when I go to build or type check with TSC on my NextJS 12 Project.

node_modules/iron-session/next/index.ts:8:48 - error TS2307: Cannot find module '../src/getPropertyDescriptorForReqSession' or its corresponding type declarations.

8 import getPropertyDescriptorForReqSession from "../src/getPropertyDescriptorForReqSession";
                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

in my tsconfig.json it looks like

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["*"]
    },
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "jsxImportSource": "@emotion/react",
    "incremental": true
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

If I remove these bits

    "baseUrl": ".",
    "paths": {
      "@/*": ["*"]
    },

Works just fine but I'd really love to be able to use alias'.

Is this something that can be changed on a package side or is it just a configuration problem on my side?

csprance avatar Feb 05 '22 02:02 csprance

Any chance this can be merged? It's also happening to us

adarnon avatar Apr 13 '22 20:04 adarnon

Hey I am confused because I also use baseUrl: "." in my projects so I am wondering what's really wrong here? If someone can reduce the necessary changes too that would be great. The current PR for this (thanks @csprance) includes multiple things I don't get why the library should be doing.

Importing a file with a relative path in the library shouldn't trigger an issue on the consumer side. Please provide some more information/dig more if possible with reference links to issue on TS/Next. Thanks!

vvo avatar Apr 13 '22 22:04 vvo

I had the same problem and could solve it by changing the import path. Before I imported modules from iron-session/next which resulted in the error described by @csprance. Changing the path to iron-session/next/dist fixed it.

zunkelty avatar Apr 15 '22 11:04 zunkelty

I'm not sure why it happens I just know this fixes it. I was using a bunch of different libs and this was the only one that had issues with baseURL.

In the end I just ended up duplicating the lib in my own codebase to deal with the changes.

csprance avatar Apr 17 '22 13:04 csprance

Hey there, you can see that our official TypeScript example is also using baseUrl as seen here: https://github.com/vvo/iron-session/blob/f9d0351e11c2553ad605f17cf1c139b79eee1db1/examples/next.js-typescript/tsconfig.json#L20 and we then import this way: https://github.com/vvo/iron-session/blob/f9d0351e11c2553ad605f17cf1c139b79eee1db1/examples/next.js-typescript/pages/api/login.ts#L4 which has no problem.

So most probably there's something different in your setup and you'll need to find what's the difference. Thanks!

vvo avatar Apr 22 '22 22:04 vvo

I don't think the issue is with the baseURL it's with the path section.

csprance avatar Apr 23 '22 00:04 csprance

Same issue here. Had to import it from /dist as well as mentioned here: https://github.com/vvo/iron-session/issues/455#issuecomment-1100040845

For context, this is an issue with Jest which isn't able to resolve ESM that well.

jamiter avatar Jul 13 '22 14:07 jamiter

It looks like the package has an import that's depending on something in the src folder, which I'm guessing is removed from the deployed package (and replaced with dist).

JulianKingman avatar Jan 12 '23 03:01 JulianKingman