serverless-webpack: V3: Unable to import module 'handler': Error

I often have this error and I never know where to look at to fix the issue. Is there a way/tool to have more information about the error ?

I have the SLS_DEBUG set to true but it doesn’t help.

Unable to import module 'handler': Error
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/var/task/handler.js:373:19)
    at __webpack_require__ (/var/task/handler.js:20:30)
    at Object.<anonymous> (/var/task/handler.js:53:19)
    at __webpack_require__ (/var/task/handler.js:20:30)
    at /var/task/handler.js:40:18
    at Object.<anonymous> (/var/task/handler.js:43:10)

Thank you very much! Thierry

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 32
  • Comments: 43 (11 by maintainers)

Most upvoted comments

In my case, making sure that ALL required modules were actually in package.json solved the issue 😃

I just ran into this issue again, and it was caused by a dependency accidentally being added to devDependencies when it should have been in dependencies.

I had this issue until I discovered I was missing "babel-runtime"in dependencies.

I’m having the same issue. I can run the function locally with $ serverless webpack invoke -f myFunction -p event.json. But when the function is deployed to AWS, every invocation fails:

Unable to import module 'handler': Error
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/task/handler.js:240:19)
at __webpack_require__ (/var/task/handler.js:20:30)
at Object.<anonymous> (/var/task/handler.js:81:19)
at __webpack_require__ (/var/task/handler.js:20:30)
at Object.defineProperty.value (/var/task/handler.js:54:17)
at __webpack_require__ (/var/task/handler.js:20:30)

@dimitrovs’s solution of adding the following option worked for me.

custom:
  webpackIncludeModules: true

Using

  • serverless-webpack 5.2.0
  • webpack 4.16.5

Also, I updated my answer as I just realized “babel-polyfill” belongs in dependencies not in devDependencies .

For anyone who casually skims the responses (like I did), the quick solution is to make sure everything you require is in your package.json under “dependencies”.

If you are still getting errors, check out dimitrovs’ issue.

@adamwilbert @hassankhan I seem to have fixed the issue I was having with webpack but modifying my webpack.config.js

Made the change of:

libraryTarget: 'commonjs2', to libraryTarget: 'commonjs',

as well as

plugins: [
    new webpack.IgnorePlugin(/mariasql/, /\/knex\//),
    new webpack.IgnorePlugin(/mssql/, /\/knex\//),
    new webpack.IgnorePlugin(/mysql/, /\/knex\//),
    new webpack.IgnorePlugin(/mysql2/, /\/knex\//),
    new webpack.IgnorePlugin(/oracle/, /\/knex\//),
    new webpack.IgnorePlugin(/oracledb/, /\/knex\//),
    new webpack.IgnorePlugin(/pg-query-stream/, /\/knex\//),
    new webpack.IgnorePlugin(/sqlite3/, /\/knex\//),
    new webpack.IgnorePlugin(/strong-oracle/, /\/knex\//),
    new webpack.IgnorePlugin(/pg-native/, /\/pg\//),
  ]

You can see the Lambda working here:

https://kirhd1mpkb.execute-api.us-west-2.amazonaws.com/production/

Just having other issues now with API timeouts when registering or logging in a user. Will work on them next.

Well the problem is webpack 4.1.0 version. I updated all the packages first that also updated webpack to latest version. Back to webpack 3.11.0 and its solved. Maybe you should emphasise this more on the documentation @HyperBrain Edit: (Finally I just noticed Webpack 4 issue now at the top of issues list. Great, spent so much on this and it was obvious from the very beginning)

@Vadorequest I agree that a better message should be provided. Maybe the plugin could error out if a required dependency is detected that appears in devDependencies.

regarding your deps - babel-runtime and source-map-support are dependencies that are used at runtime and not build time, so they must be in the dependencies section.

For domain-manager, I don’t know if there are any runtime function in there.

Glad to here that it works now 😃 . Indeed setting webpackIncludeModules to false or omitting it completely will disable any module packaging.

Maybe the default should be true, even if not set, as this is the most likely case that users want to use.

For reference, I experienced this issue because I used baseUrl in ts-config but forgot to include NODE_PATH as env variables.

I added NODE_PATH as my env variable and resolved an issue.