question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serverless build fails with: ModuleNotFoundError: Module not found: Error: Can't resolve 'aws-sdk'

See original GitHub issue

Bug report

Describe the bug

When doing a serverless build, the build fails with ModuleNotFoundError: Module not found: Error: Can't resolve 'aws-sdk' I’ve trace it down to the bcrypt module being used in an api function. I assume since this module a server function running on the server side it should work. Running a normal server build does not produce any errors.

Here is the full error:

> next build

info  - Creating an optimized production build
Failed to compile.

ModuleNotFoundError: Module not found: Error: Can't resolve 'aws-sdk' in '/home/mike/test/nextjs-serverless-build/node_modules/node-pre-gyp/lib'


> Build error occurred
Error: > Build failed because of webpack errors
    at build (/home/mike/test/nextjs-serverless-build/node_modules/next/dist/build/index.js:15:918)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test@0.1.0 build: `next build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the test@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/mike/.npm/_logs/2020-08-27T03_25_35_982Z-debug.log

To Reproduce

I’ve created a repo from a barebones create-next-app example. The only change is pages/api/hello.js uses the bcrypt module and the next.config.js is set to serverless.

https://github.com/mikecao/nextjs-serverless-build

Expected behavior

Should not produce an error.

System information

  • OS: Ubuntu 20.04
  • Version of Next.js: 9.5.2
  • Version of Node.js: 14.8.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

3reactions
thiskevinwangcommented, Mar 5, 2021

Just a +1: You can expect this error to occur when creating a serverless build with say, a NextJS + MDX app that uses remark-prism

  • it’ll complain about 'canvas', in jsdom
  • if you go through your yarn.lock, you can see that jsdom is used by remark-prism
2reactions
CryogenicPlanetcommented, Feb 13, 2021

Does anybody have a workaround for this? Thank you.

@lednhatkhanh

I got it to work with adding a custom webpack config for next.js and adding externals

// next.config.js

 webpack(config, options) {

const externals = {
        ...config.externals,
        bcrypt: 'bcrypt',
        jimp: 'jimp',
        'probe-image-size': 'probe-image-size'
      }
  return {
        ...config,
        module: {
          ...config.module,
          rules: [...config.module.rules, ...rules]
        },
        externals
      }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Serverless Framework with AWS Lambda error "Cannot find ...
I have the same problem with serverless framework to deploy multiple lambda functions. I fixed by the following steps.
Read more >
Nextjs deploy error after installing remark-prism package
Serverless build fails with: ModuleNotFoundError: Module not found: Error: Can't resolve 'aws-sdk' The cause is that, the dependency needs ...
Read more >
Serverless Webpack - Serverless Framework: Plugins
A Serverless Framework plugin to build your lambda functions with Webpack. ... All settings are optional and will be set to reasonable defaults...
Read more >
Troubleshoot deployment issues in Lambda
General: Cannot find, cannot load, unable to import, class not found, no such file or directory. Error: Cannot find module 'function'. Error: cannot...
Read more >
How do I resolve a 'module not found' error? - Vercel
The 'module not found' error is a syntax error that appears when the static import statement cannot find the file at the declared...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found