nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.96k stars 3.52k forks source link

Express App Crashes due to Error [ERR_PACKAGE_PATH_NOT_EXPORTED] #9573

Closed asierbayon closed 10 months ago

asierbayon commented 10 months ago

Environment

System: OS: macOS 13.3.1 CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Memory: 8.91 GB / 64.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 16.19.1 - ~/.nvm/versions/node/v16.19.1/bin/node Yarn: 1.22.10 - /usr/local/bin/yarn npm: 8.19.3 - ~/.nvm/versions/node/v16.19.1/bin/npm Browsers: Brave Browser: 119.1.60.114 Chrome: 120.0.6099.129 Safari: 16.4 npmPackages: @auth/express: ^0.1.2 => 0.1.2

Reproduction URL

https://github.com/asierbayon/auth-express-bug-demo

Describe the issue

Whenever @auth/express or even @auth/core is imported into an Express project, the following error is shown:

throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined

This happens in Typescript projects once the code is compiled and in Javascript projects too.

How to reproduce

yarn && yarn dev

Expected behavior

The app does not crash.

balazsorban44 commented 10 months ago

@auth/express is ESM-only, it looks like you are trying to require-ing it.

Our ESM configuration has no errors, see:

https://publint.dev/@auth/express@0.1.2

solo8116 commented 10 months ago

I am using typescript and sample code of the docs but the same error

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in \node_modules\@auth\express\package.json
    at new NodeError (node:internal/errors:405:5)
    at exportsNotFound (node:internal/modules/esm/resolve:359:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:639:13)
    at resolveExports (node:internal/modules/cjs/loader:567:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:636:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1063:27)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (AppData\Local\npm-cache\_npx\1bf7c3c15bf47d04\node_modules\@cspotcode\source-map-support\source-map-support.js:811:30)
    at Function.Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
asierbayon commented 10 months ago

@solo8116 you need to use ES Modules on your Express project. Unfortunately, most examples you'll see online are not configured like this.

Here's what you should do if you're using Typescript:

tsconfig.json example

{
    "compilerOptions": {
        "target": "ES2022",
        "module": "ES2022",
        "lib": ["ES2022", "dom", "DOM.Iterable"],
        "allowJs": true,
        "checkJs": true,
        "outDir": "./dist",
        "rootDir": "./src",
        "removeComments": true,
        "moduleResolution": "node",
        "pretty": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "skipLibCheck": true,
        "typeRoots": ["node_modules/@types"]
    },
    "include": ["src/**/*.ts"],
    "exclude": ["node_modules"]
}

also include "type": "module" on your package.json

You'll need also to declare the file extension on every import, you can help yourself to do this enforcing it via eslint

.eslintrc

{
    "parser": "@typescript-eslint/parser",
    "extends": [
        "plugin:@typescript-eslint/recommended",
        "plugin:import/errors",
        "plugin:import/warnings",
        "plugin:import/typescript"
    ],
    "settings": {
        "import/resolver": {
            "typescript": {
                "alwaysTryTypes": true
            }
        }
    },
    "rules": {
        "import/no-unresolved": "error",
        "import/extensions": [
            "error",
            "ignorePackages",
            {
                "ts": "never",
                "tsx": "never"
            }
        ]
    }
}

Remember to install also the relevant deps to make eslint and its plugin work.

This should be enough, I hope :)

RosePinkDragon commented 9 months ago

nope not helping, I tried changing the ts-config and adding in the eslint config as suggested by @asierbayon still no help, I have created a minimal reproduction for prisma and express. it will be great if you take a look, please take a look at this.

I couldn't find a fix, will continue on this, i currently think it has something to do with tsconfig: ipfs github

I have tried other packages also but can't get it working all give the same error's and only in authjs rest of the packages work as they are.

also there are some same issues with different packages: #8597, #8727

edit: it's definitely a ts issue, reverting to js, fixed the issue.

tmax22 commented 8 months ago

I got this issue too. to anyone getting it make sure you have "type": "module" in package.json file

moonrailgun commented 5 months ago

i cannot upgrade my project to "type": "module". too much problem and pain.

is any other ideas?