blitz: Unable to use next-auth with provider: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]
What is the problem?
Hi there,
I am currently unable to configure a next-auth provider (EmailProvider) on blitz. I followed the configuration steps that are available in the docs (https://blitzjs.com/docs/next-auth), but this error occurs when I try to access the auth link (api/auth/email (or any other provider)/login).
The following error is what I keep getting: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './core/init' is not defined by "exports" in /Users/imadarchid/Desktop/Repositories/fire/node_modules/next-auth/package.json imported from /Users/imadarchid/Desktop/Repositories/fire/node_modules/@blitzjs/auth/dist/next-auth.cjs
Paste all your error logs here:
{
"stack": "Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './core/init' is not defined by \"exports\" in /Users/imadarchid/Desktop/Repositories/fire/node_modules/next-auth/package.json imported from /Users/imadarchid/Desktop/Repositories/fire/node_modules/@blitzjs/auth/dist/next-auth.cjs\n at new NodeError (node:internal/errors:387:5)\n at throwExportsNotFound (node:internal/modules/esm/resolve:365:9)\n at packageExportsResolve (node:internal/modules/esm/resolve:649:3)\n at packageResolve (node:internal/modules/esm/resolve:833:14)\n at moduleResolve (node:internal/modules/esm/resolve:901:20)\n at defaultResolve (node:internal/modules/esm/resolve:1115:11)\n at nextResolve (node:internal/modules/esm/loader:163:28)\n at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)\n at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)\n at ESMLoader.import (node:internal/modules/esm/loader:525:22)\n at importModuleDynamically (node:internal/modules/cjs/loader:1129:29)\n at importModuleDynamicallyWrapper (node:internal/vm/module:438:21)\n at importModuleDynamically (node:vm:389:46)\n at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)\n at /Users/imadarchid/Desktop/Repositories/fire/node_modules/@blitzjs/auth/dist/next-auth.cjs:354:24\n at Generator.next (<anonymous>)\n at fulfilled (/Users/imadarchid/Desktop/Repositories/fire/node_modules/@blitzjs/auth/dist/next-auth.cjs:273:24)\n at processTicksAndRejections (node:internal/process/task_queues:96:5)",
"message": "Package subpath './core/init' is not defined by \"exports\" in /Users/imadarchid/Desktop/Repositories/fire/node_modules/next-auth/package.json imported from /Users/imadarchid/Desktop/Repositories/fire/node_modules/@blitzjs/auth/dist/next-auth.cjs",
"code": "ERR_PACKAGE_PATH_NOT_EXPORTED"
}
Paste all relevant code snippets here:
// src/pages/api/auth[...nextauth].ts
import { api } from "src/blitz-server"
import EmailProvider from "next-auth/providers/email"
import { NextAuthAdapter, BlitzNextAuthOptions } from "@blitzjs/auth/next-auth"
import db, { User } from "db"
import { Role } from "types"
// Has to be defined separately for `profile` to be correctly typed below
const providers = [
EmailProvider({
from: process.env.GITHUB_CLIENT_ID as string,
server: process.env.GITHUB_CLIENT_SECRET as string,
}),
]
export default api(
NextAuthAdapter({
successRedirectUrl: "/",
errorRedirectUrl: "/error",
providers,
callback: async (user, account, profile, session) => {
console.log("USER SIDE PROFILE_DATA", { user, account, profile })
let newUser: User
try {
newUser = await db.user.findFirstOrThrow({ where: { name: { equals: user.name } } })
} catch (e) {
newUser = await db.user.create({
data: {
email: user.email as string,
name: user.name as string,
role: "USER",
},
})
}
const publicData = {
userId: newUser.id,
role: newUser.role as Role,
source: "email",
}
await session.$create(publicData)
return { redirectUrl: "/" }
},
})
)
// blitz-server.ts
import { setupBlitzServer } from "@blitzjs/next"
import { AuthServerPlugin, PrismaStorage } from "@blitzjs/auth"
import { simpleRolesIsAuthorized } from "@blitzjs/auth"
import { BlitzLogger } from "blitz"
import db from "db"
import { authConfig } from "./blitz-client"
export const { gSSP, gSP, api, useAuthenticatedBlitzContext } = setupBlitzServer({
plugins: [
AuthServerPlugin({
...authConfig,
storage: PrismaStorage(db),
isAuthorized: simpleRolesIsAuthorized,
}),
],
logger: BlitzLogger({}),
})
// blitz-client.ts
import { AuthClientPlugin } from "@blitzjs/auth"
import { setupBlitzClient } from "@blitzjs/next"
import { BlitzRpcPlugin } from "@blitzjs/rpc"
export const authConfig = {
cookiePrefix: "fire",
}
export const { withBlitz, BlitzProvider } = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "web-cookie-prefix",
}),
BlitzRpcPlugin({}),
],
})
// next.config.js
const { withNextAuthAdapter } = require("@blitzjs/auth/next-auth")
const { withBlitz } = require("@blitzjs/next")
/**
* @type {import('@blitzjs/next').BlitzConfig}
**/
const config = {}
module.exports = withBlitz(withNextAuthAdapter(config))
What are detailed steps to reproduce this?
- Run a fresh instance of blitz
- Follow the steps on https://blitzjs.com/docs/next-auth
- Try to login via any provider
Run blitz -v
and paste the output here:
Blitz version: 2.0.0-beta.27 (global)
Blitz version: 2.0.0-beta.27 (local)
macOS Ventura | darwin-arm64 | Node: v16.19.1
Package manager: npm
System:
OS: macOS 13.3.1
CPU: (10) arm64 Apple M2 Pro
Memory: 933.28 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 16.19.1 - ~/.nvm/versions/node/v16.19.1/bin/node
Yarn: Not Found
npm: 8.19.3 - ~/.nvm/versions/node/v16.19.1/bin/npm
npmPackages:
@blitzjs/auth: 2.0.0-beta.27 => 2.0.0-beta.27
@blitzjs/next: 2.0.0-beta.27 => 2.0.0-beta.27
@blitzjs/rpc: 2.0.0-beta.27 => 2.0.0-beta.27
@prisma/client: 4.6.1 => 4.6.1
blitz: 2.0.0-beta.27 => 2.0.0-beta.27
next: ^13.3.0 => 13.4.1
prisma: 4.6.1 => 4.6.1
react: 18.2.0 => 18.2.0
react-dom: 18.2.0 => 18.2.0
typescript: ^4.8.4 => 4.9.5
Please include below any other applicable logs and screenshots that show your problem:
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 1
- Comments: 17 (7 by maintainers)
@elsueno The main problem is
next-auth
do not exportcore
module inpackage.json
. Try to add core export to next-auth package,json file in node_modules same as image below to make it work. This happened because filenext-auth.cjs
in@blitzjs/auth
module importnext-auth/core/init
. I think we need to ask next-auth open a PR to export core module to fix this problem forever. One more thing in official docs is importwithNextAuthAdapter
from@blitzjs/auth
not from@blitzjs/auth/next-auth
.The problem is still present with Blitz 2.0.0-beta.33. Is there any solution in sight?
@tordans I did try the same but encountering issues with
patch-pacakge
first, it could not find any changes when I ranyarn patch-package next-auth
so after digging I was able to make it work with this commandyarn patch-package next-auth --exclude
. Thanks a lot for the examples and this solved the error, you saved me from creating the project from scratch.@msShanky our current workaround is to patch the package. This turned out to be the better solution on different operating systems then the
sed
-workaround described above.postinstall
https://github.com/FixMyBerlin/atlas-app/blob/develop/package.json#L41patch-package
https://github.com/FixMyBerlin/atlas-app/blob/develop/package.json#L114The nice thing about this is, once you update something you get a notice to update the patch.
I’ve added the following
"prepare"
command to my scripts list, which will add the required exports every time install is called 🤷♂️Obviously less than ideal, but it hopefully won’t immediately break any time package.json changes.
Thanks for the reproduction, I will take a look and let you know