Closed huksley closed 3 years ago
This is a problem with way the app is bundled, if you include package.json in the list of files that get bundled and deployed and that should resolve the issue - I think there are some old issues (possibly closed now) that cover this.
I think not using optional require has the impact of introducing problems for other users, which IIRC is why we don't do it - dynamic imports - which are the "right" way to solve this " - are still not well supported by some bundlers (not really NextAuth.js problem exactly, except that people would then log bug reports about that). I think there is more info about that in the old tickets.
The error isn't great and it would be nice to have a better way of doing this that is lower friction for everyone.
Splitting the SQL Adaptor from the MongoDB Adaptor (but sharing common code, like the models) is also an option and might be the best move.
PS: Thanks for recording this and for the detail. Happy to leave this open until we have a more satisfactory resolution - either code change or documentation improvement, etc.
As far as I understand, this also affects production builds for NextJS application, so would this mean that we can not use MongoDB in production?
Would be great to have documentation updated for this.
I see there is an alternative for this, is to move this piece of code into the userland, as a callback? i.e.
if (ObjectId && !(id instanceof ObjectId))
id = ObjectId(id);
}
I created a more sane patch for webpack, see this PR for more information. https://github.com/huksley/next-auth-example/pull/2/files
As far as I understand, this also affects production builds for NextJS application, so would this mean that we can not use MongoDB in production?
No it's working fine for most people.
Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep it open. (Read more at #912) Thanks!
Hi there! It looks like this issue hasn't had any activity for a while. To keep things tidy, I am going to close this issue for now. If you think your issue is still relevant, just leave a comment and I will reopen it. (Read more at #912) Thanks!
This is not work for me, I tring bild on aws amplify. I'm not using mongodb, but this erro r persists. I'm using cognito for auth. I updated already, I'm using nextjs 11, and nxt-auth 3.27.3, I tried using this dependency installed on project, typeorm, but only got fails. On build locally I not got this error.
Error: Command failed with exit code 1: node_modules/.bin/next build
warn - No ESLint configuration detected. Run next lint to begin setup
warn - Tailwind is not purging unused styles because no template paths have been provided.
warn - If you have manually configured PurgeCSS outside of Tailwind or are deliberately not removing unused styles, set `purge: false` in your Tailwind config file to silence this warning.
warn - https://tailwindcss.com/docs/controlling-file-size/#removing-unused-css
Failed to compile.
ModuleNotFoundError: Module not found: Error: Can't resolve 'mongodb' in '/codebuild/output/src095445125/src/bbfront/node_modules/typeorm/platform'
> Build error occurred
Error: > Build failed because of webpack errors
at /codebuild/output/src095445125/src/bbfront/node_modules/next/dist/build/index.js:15:924
at async Span.traceAsyncFn (/codebuild/output/src095445125/src/bbfront/node_modules/next/dist/telemetry/trace/trace.js:6:584)
info - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
info - Checking validity of types...
info - Creating an optimized production build...
at makeError (/root/.//node_modules/execa/lib/error.js:60:11)
at handlePromise (/root/.//node_modules/execa/index.js:118:26)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Builder.build (/root/.//node_modules/@sls-next/lambda-at-edge/dist/build.js:348:13) {
shortMessage: 'Command failed with exit code 1: node_modules/.bin/next build',
command: 'node_modules/.bin/next build',
escapedCommand: '"node_modules/.bin/next" build',
exitCode: 1,
signal: undefined,
signalDescription: undefined,
stdout: 'info - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5\n' +
'info - Checking validity of types...\n' +
'info - Creating an optimized production build...',
stderr: 'warn - No ESLint configuration detected. Run next lint to begin setup\n' +
'\n' +
'warn - Tailwind is not purging unused styles because no template paths have been provided.\n' +
'warn - If you have manually configured PurgeCSS outside of Tailwind or are deliberately not removing unused styles, set `purge: false` in your Tailwind config file to silence this warning.\n' +
'warn - https://tailwindcss.com/docs/controlling-file-size/#removing-unused-css\n' +
'Failed to compile.\n' +
'\n' +
"ModuleNotFoundError: Module not found: Error: Can't resolve 'mongodb' in '/codebuild/output/src095445125/src/bbfront/node_modules/typeorm/platform'\n" +
'\n' +
'\n' +
'> Build error occurred\n' +
'Error: > Build failed because of webpack errors\n' +
' at /codebuild/output/src095445125/src/bbfront/node_modules/next/dist/build/index.js:15:924\n' +
' at async Span.traceAsyncFn (/codebuild/output/src095445125/src/bbfront/node_modules/next/dist/telemetry/trace/trace.js:6:584)',
failed: true,
timedOut: false,
isCanceled: false,
killed: false
}
Describe the bug
When using MongoDB storage adapter, the way right now it is loaded by using require_optional are not compatible with
serverless-nextjs
packaging (and I believe not really compatible with AWS Lambda ways of working).When it packaged as Lambda@Edge there is no package.json anymore.
It manifests as following error in login form: Try signing with a different account.
Also error generated in AWS CloudWatch (see below)
Steps to reproduce Clone https://github.com/huksley/next-auth-example/tree/aws-serverless (notice aws-serverless branch)
Configure
.env.local
with at least following defined:You can get MongoDB instance from MongoDB Cloud Atlas for free.
Run once with
npx serverless
to do initial deploy and get CloudFront domain.Add
NEXTAUTH_URL=https://xxxxx.cloudfront.net
to .env.local afterwards and deploy again.Navigate to
https://xxxxx.cloudfront.net
and try to login with Google.Expected behavior
Google Login works with MongoDB storage adapter.
Screenshots or error logs
Additional context
It is working fine locally by using
npm run dev
but NOT WITHnpm run build && npm run start
It is connected with the issue discussed here: https://github.com/nextauthjs/next-auth/issues/552
Not sure why
next-auth
needs to loadmongodb
as an optional dependency when it will clearly fail ifconfig.type == "mongodb"
Hot fixing the bug Patching next-auth fixes this problem:
Feedback Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.