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
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)
I have the same problem as well. Able to run locally, but fails when deployed.
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:533:19)
at __webpack_require__ (/var/task/handler.js:20:30)
at Object.<anonymous> (/var/task/handler.js:72:18)
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)
Anyone know how to fix?
Hmm, it's always __webpack_require__(6); that fails for me. I tried swapping the order of some of the requires and it still failed on the same place. __webpack_require__() one through five seems to be babel-runtime requires, number six is the first "real" require.
In my case, making sure that ALL required modules were actually in package.json solved the issue :)
yeah that error report doesn't help much. I'm closing this hoping that the last @msl-kabo messages fixes it for everyone.
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 am having the same problem. serverless offline start works fine, but when deployed to AWS I get the same error. I have 4 handlers which are all in a sub-directory, I am not sure if this makes a difference. Here is my webpack.config.js:
const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
module.exports = {
entry: slsw.lib.entries,
target: 'node',
externals: [nodeExternals()],
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
}
};
my package.json:
"dependencies": {
"babel-runtime": "^6.25.0",
"jsonwebtoken": "^7.4.2",
"jwks-rsa": "^1.2.0",
"request": "^2.81.0",
"uuid": "^2.0.3"
},
"devDependencies": {
"aws-sdk": "^2.98.0",
"serverless-offline": "^3.15.3",
"serverless-webpack": "^3.0.0-rc.1",
"webpack": "^3.5.4",
"webpack-node-externals": "^1.6.0"
}
Webpack also shows this message in console:
[0] external "aws-sdk" 42 bytes {0} {1} {2} {3} {4} [not cacheable]
[1] external "request" 42 bytes {0} {1} {2} {3} {4} [not cacheable]
I intentionally put aws-sdk in devDependencies because it's already available on AWS. There is no node_modules in the deployment zip.
@dimitrovs Can you try with the V3 RC? You can use it with ^3.0.0-rc.1. The packaging has been improved and stabilized there. Additionally serverless invoke local is now supported to run a single function locally with an event JSON.
From your config files I do not see any issues (putting aws-sdk into the devDependencies is ok and will prevent it to be packaged). Can you additionally post the function/handler definition from your serverless.yml?
@HyperBrain you can see in my package.json that I am using the RC.
Serverless.yml:
list:
handler: projects/list.list
events:
- http:
path: projects
method: get
cors: true
authorizer:
arn: ...
If webpack only shows the two externals:
[0] external "aws-sdk" 42 bytes {0} {1} {2} {3} {4} [not cacheable]
[1] external "request" 42 bytes {0} {1} {2} {3} {4} [not cacheable]
aws-sdk is a dev dependency and thus will not be packaged, but request should be packaged though. The other ones seem not to be used by the function (according to webpack).
Can you provide a sample project (on GitHub or Gist) with the minimal contents with that the issue is reproducible?
BTW: Which Node version do you use to build and deploy? Additionally you can invoke serverless with the --verbose switch. This should show some more information from the module packaging phase.
You are right, I checked my code and the only external dependency I am currently using is request, I removed the other ones from package.json now. My Node version is v6.11.0 . I copied all the config from serverless-offline and it works now, you can close the issue I guess. I think the problem was solved by adding:
custom:
webpackIncludeModules: true
to serverless.yml and:
module: {
loaders: [{
test: /\.js$/,
loaders: ['babel-loader'],
include: __dirname,
exclude: /node_modules/,
}],
},
to webpack.config.js and:
"babel-polyfill": "^6.23.0",
to package.json dependencies and:
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"aws-sdk": "^2.98.0",
"serverless": "^1.19.0",
to devDependencies .
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 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.
Also, I updated my answer as I just realized "babel-polyfill" belongs in dependencies not in devDependencies .
I have the following dependencies:
"devDependencies": {
"babel-core": "6.26.0",
"babel-loader": "7.1.2",
"babel-plugin-source-map-support": "2.0.0",
"babel-plugin-transform-runtime": "6.23.0",
"babel-preset-env": "1.6.1",
"babel-preset-stage-3": "6.24.1",
"serverless-webpack": "4.3.0",
"webpack": "3.11.0",
"webpack-node-externals": "1.6.0"
},
"dependencies": {
"@awspilot/dynamodb": "1.0.9",
"aws-sdk": "2.194.0",
"babel-runtime": "6.26.0",
"jest": "22.3.0",
"moment": "2.20.1",
"serverless-domain-manager": "2.3.0",
"source-map-support": "0.5.3",
"uuid": "3.2.1",
"uuid-validate": "0.0.2"
}
I moved babel-runtime, jest, serverless-domain-manager and source-map-support to devDependencies and I obviously ran into the same issue.
How can we figure out what should be in dependencies and what should be in devDependencies ?
I don't see another solution than looking at the repo, for each dependency, which is really time consuming... A better error message is definitely a must have, without this github issue we'd be lost...
@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.
Eventually, only babel-runtime and source-map-support were needed in this case, but I had to go to the repo to make sure of that.
Im still having the same issue. Babel runtime and source-map-support are at dependencies and aws-sdk and webpack etc are at devDependencies.
serverless.yml has
custom:
webpackIncludeModules: true
webpack.config.js is
module.exports = {
entry: slsw.lib.entries,
target: 'node',
devtool: 'source-map',
externals: [nodeExternals()],
module: {
rules: [{
test: /\.js$/,
loader: 'babel-loader',
include: __dirname,
exclude: /node_modules/,
}]
}
};
Yet no node_modules folder is exported to .zip file in .serverless folder and running 'serverless package' command. Its like webpackIncludeModules field in yml has NO effect at all. All the package and npm versions are up-to-date
It can find the external packages but they are not in zip file

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)
@Can-Sahin If you want to see what versions I use, take a look at https://github.com/Vadorequest/serverless-with-next (updated yesterday)
Since I only use specific version I avoid running into this kind of issues.
@Can-Sahin @Vadorequest There is a preview version of the webpack plugin available that supports webpack 4 (see #331). You can try 5.5.0-rc.1 from npm and it should work as expected. I already switched one of our projects to Webpack 4 and it works as expected with this version.
The plugin's 4.x.x versions only support Webpack up to version 3.
However, you also have to update the various Webpack loaders and plugins to versions that support it.
@dimitrovs's solution of adding the following option worked for me.
custom:
webpackIncludeModules: true
Using
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.
I was on a Mac, and my exports were case insensitive, so the local invoke worked. Easy to overlook!
Hi @HyperBrain,
I am running into this issue as well with a typescript app.
I have created a complete repo here for reference.
I can access and use the app locally via sls offline, however when I deploy I am getting the error:
Unable to import module 'src/lambda': Error
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.module.exports.i.parse.a (/var/task/src/lambda.js:35:43367)
at a (/var/task/src/lambda.js:1:186)
at Object.<anonymous> (/var/task/src/lambda.js:74:44217)
at a (/var/task/src/lambda.js:1:186)
at Object.module.exports.i.__esModule (/var/task/src/lambda.js:74:44104)
at a (/var/task/src/lambda.js:1:186)
which seems consistent with the experiences of the folks above.
In order to try and debug in production myself I simply moved all the modules from devDependencies into dependencies and deployed to Lambda, but ran into the same issue.
Any help or insight would be greatly appreciated.
Kind regards,
Luke
@lukebyrne could you try changing this file's export from module.exports.handler to export const handler? I have a feeling your issue may be related to #486...
@hassankhan Thanks for the prompt response, I tried your suggestion but to no avail.
Error still the same via the logs:
Unable to import module 'src/lambda': Error
sls offline still works fine with that suggested change though.
@hassankhan Feels like this issue could be a pretty biggish one for users of serverless-webpack as I get the feeling a lot of people are playing around with both webpack and typescript and the serverless framework.
Really appreciate your assistance so far.
@hassankhan I updated the repo https://github.com/lukebyrne/typescript-express-jwt-lambda to reflect your suggestions
@lukebyrne
@hassankhan I updated the repo https://github.com/lukebyrne/typescript-express-jwt-lambda to reflect your suggestions
I was having similar issues, noticed in your repo you're using yarn. i resolved my problem with
custom:
webpack:
packager: "yarn"
in my serverless.yml
Apparently it was by default getting set to npm and causing some problems. not sure why that would be the issue, but figured I'd throw it out there to see if it helps!
@adamwilbert Updated in the repo but no luck. Do you think you could pull down the repo and see what happens when you deploy it?
@lukebyrne could you try changing this file's export from
module.exports.handlertoexport const handler? I have a feeling your issue may be related to #486...
I think it might an issue with knex in my webpack.config.js
https://github.com/lukebyrne/typescript-express-jwt-lambda/blob/master/webpack.config.js
I was googling around saw on a few Github issues that you have had similar issues with knex, any suggestions?
I am thinking that my sls deploy is not packaging up knex correctly and hence why its falling over on production.
@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.
Glad you finally got it working in the end @lukebyrne 👍
Out of interest, how did you initially create the project? I use the template that comes with Serverless (sls create --template aws-nodejs-typescript) which gets me going pretty quickly.
@hassankhan I think I did set it up using the sls create --template aws-nodejs-typescript.
It was the knex issues that were killing me.
I have now bumped up against async/await issues while refactoring then() promise returns as per this article https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9
I will have a look at this article to find away around https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/
@hassankhan I have now run into the same Knex issues that it appears that you have been experiencing (seen your comments in issues threads)
Would you have any working Lambda JS/TS Express/Knex implementation that you could share?
I have set the context.callbackWaitsForEmptyEventLoop = false but am having no joy. Could really use some guidance.
@lukebyrne IIRC my issue was keeping connections open, this article was a massive help.
@hassankhan I read the article but it hasnt helped me progress. Any chance you have boilerplate app lying about I code take a gander at?
Getting the same error that just started recently for some reason.
Here is the error:
Unable to import module 'app': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/app.js:176:18)
at __webpack_require__ (/var/task/app.js:21:30)
at Module.<anonymous> (/var/task/app.js:216:27)
at __webpack_require__ (/var/task/app.js:21:30)
at /var/task/app.js:85:18
at Object.<anonymous> (/var/task/app.js:88:10)
Here is my serverless.yml file:
# Use the serverless-webpack plugin to transpile ES6
plugins:
- serverless-webpack
- serverless-domain-manager
# serverless-webpack configuration
# Enable auto-packing of external modules
custom:
webpack:
webpackConfig: ./webpack.config.js
includeModules: true
provider:
name: aws
runtime: nodejs8.10
stage: prod
apiName: application-prod
region: us-east-1
exclude:
- .git/**
- .env
- env.yml
- app.dev.js
- server.key
- server.crt
environment: ${file(env.yml):${self:provider.stage}}
functions:
app:
handler: app.handler
events:
- http:
path: /
method: any
cors:
origin: 'https://www.example.co' # <-- Specify allowed origin
allowCredentials: true
- http:
path: '{proxy+}'
method: any
cors:
origin: 'https://www.example.co' # <-- Specify allowed origin
allowCredentials: true
Here is my package.json file:
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/node": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-proposal-object-rest-spread": "^7.4.3",
"@babel/plugin-transform-runtime": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.7.0",
"babel-loader": "^8.0.5",
"cross-env": "^5.2.0",
"dotenv": "^6.2.0",
"eslint": "^5.16.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.15.0",
"eslint-plugin-jsx-a11y": "^6.2.0",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.13.0",
"jest": "^24.7.0",
"mongodb-memory-server": "^3.1.3",
"morgan": "~1.9.0",
"nock": "^10.0.6",
"nodemon": "^1.18.9",
"prettier": "^1.16.4",
"serverless-domain-manager": "^2.6.13",
"serverless-webpack": "^5.2.0",
"webpack": "^4.16.2",
"webpack-node-externals": "^1.7.2"
},
"dependencies": {
"@babel/polyfill": "^7.4.3",
"@babel/runtime": "^7.4.3",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"csvtojson": "^2.0.8",
"elasticsearch": "^15.4.1",
"express": "~4.16.0",
"helmet": "^3.16.0",
"jsonwebtoken": "^8.5.1",
"moment": "^2.24.0",
"mongodb": "^3.2.2",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"serverless-http": "^1.9.1",
"source-map-support": "^0.5.12",
"validator": "^10.11.0"
}
}
and my webpack.config.js file:
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
module.exports = {
entry: slsw.lib.entries,
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
target: 'node',
devtool: slsw.lib.webpack.isLocal ? 'inline-source-map' : 'source-map',
externals: [nodeExternals()],
optimization: {
minimize: false,
},
performance: {
hints: false,
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: __dirname,
exclude: /node_modules/,
},
],
},
};
So far I have tried:
The weird part is that no matter which modules I put in dependencies I always get the same result on sls package. Here is the output:
Time: 1133ms
Built at: 05/30/2019 2:42:57 PM
Asset Size Chunks Chunk Names
app.js 31.3 KiB 0 [emitted] app
app.js.map 50.3 KiB 0 [emitted] app
Entrypoint app = app.js app.js.map
[1] external "express" 42 bytes {0} [built]
[2] external "passport" 42 bytes {0} [built]
[3] external "bcryptjs" 42 bytes {0} [built]
[5] external "cors" 42 bytes {0} [built]
[6] external "cookie-parser" 42 bytes {0} [built]
[7] external "helmet" 42 bytes {0} [built]
[8] external "serverless-http" 42 bytes {0} [built]
[9] external "passport-jwt" 42 bytes {0} [built]
[14] external "core-js/modules/es7.array.flat-map" 42 bytes {0} [built]
[15] external "core-js/modules/es6.array.sort" 42 bytes {0} [built]
[16] external "core-js/modules/es7.promise.finally" 42 bytes {0} [built]
[17] external "core-js/modules/es7.symbol.async-iterator" 42 bytes {0} [built]
[18] external "core-js/modules/es7.string.trim-left" 42 bytes {0} [built]
[19] external "core-js/modules/es7.string.trim-right" 42 bytes {0} [built]
[20] ./app.js + 13 modules 22.9 KiB {0} [built]
| ./app.js 1.29 KiB [built]
| ./utils/auth/passport.js 529 bytes [built]
| ./routes/users.js 771 bytes [built]
| ./routes/search.js 690 bytes [built]
| ./controllers/users.js 4.81 KiB [built]
| ./controllers/search.js 3.29 KiB [built]
| ./utils/validation/users.js 5.37 KiB [built]
| ./models/users.js 1.47 KiB [built]
| ./utils/search/elasticsearchConfig.js 152 bytes [built]
| ./models/search.js 929 bytes [built]
| ./utils/search/elasticsearch.js 2.3 KiB [built]
| ./utils/validation/isEmpty.js 335 bytes [built]
| ./utils/validation/recaptcha.js 498 bytes [built]
| ./utils/db/mongodb.js 476 bytes [built]
+ 6 hidden modules
We should check which one of the merges from 5.2.0 to 5.3.0 introduced the
regression.
BTW: There is a separate bug entry for the reintroduced issue (starts with
5.3.0: XXXX).
We should put all discussion in there.
On Thu, May 30, 2019 at 8:50 PM Nick Bordeau notifications@github.com
wrote:
Getting the same error that just started recently for some reason.
Here is the error:
Unable to import module 'app': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.(/var/task/app.js:176:18)
at __webpack_require__ (/var/task/app.js:21:30)
at Module.(/var/task/app.js:216:27)
at __webpack_require__ (/var/task/app.js:21:30)
at /var/task/app.js:85:18
at Object.(/var/task/app.js:88:10) Here is my serverless.yml file:
Use the serverless-webpack plugin to transpile ES6
plugins:
- serverless-webpack
- serverless-domain-manager
serverless-webpack configuration
Enable auto-packing of external modules
custom:
webpack:
webpackConfig: ./webpack.config.js
includeModules: trueprovider:
name: aws
runtime: nodejs8.10
stage: prod
apiName: partsync-prod
region: us-east-1
exclude:
- .git/**
- .env
- env.yml
- app.dev.js
- server.key
- server.crt
environment: ${file(env.yml):${self:provider.stage}}functions:
app:
handler: app.handler
events:
- http:
path: /
method: any
cors:
origin: 'https://www.example.co' # <-- Specify allowed origin
allowCredentials: true
- http:
path: '{proxy+}'
method: any
cors:
origin: 'https://www.example.co' # <-- Specify allowed origin
allowCredentials: trueHere is my package.json file:
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/node": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-proposal-object-rest-spread": "^7.4.3",
"@babel/plugin-transform-runtime": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.7.0",
"babel-loader": "^8.0.5",
"cross-env": "^5.2.0",
"dotenv": "^6.2.0",
"eslint": "^5.16.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.15.0",
"eslint-plugin-jsx-a11y": "^6.2.0",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.13.0",
"jest": "^24.7.0",
"mongodb-memory-server": "^3.1.3",
"morgan": "~1.9.0",
"nock": "^10.0.6",
"nodemon": "^1.18.9",
"prettier": "^1.16.4",
"serverless-domain-manager": "^2.6.13",
"serverless-webpack": "^5.2.0",
"webpack": "^4.16.2",
"webpack-node-externals": "^1.7.2"
},
"dependencies": {
"@babel/polyfill": "^7.4.3",
"@babel/runtime": "^7.4.3",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"csvtojson": "^2.0.8",
"elasticsearch": "^15.4.1",
"express": "~4.16.0",
"helmet": "^3.16.0",
"jsonwebtoken": "^8.5.1",
"moment": "^2.24.0",
"mongodb": "^3.2.2",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"serverless-http": "^1.9.1",
"source-map-support": "^0.5.12",
"validator": "^10.11.0"
}
}and my webpack.config.js file:
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');module.exports = {
entry: slsw.lib.entries,
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
target: 'node',
devtool: slsw.lib.webpack.isLocal ? 'inline-source-map' : 'source-map',
externals: [nodeExternals()],
optimization: {
minimize: false,
},
performance: {
hints: false,
},
module: {
rules: [
{
test: /.js$/,
loader: 'babel-loader',
include: __dirname,
exclude: /node_modules/,
},
],
},
};So far I have tried:
- Verifying that all dependiencies are installed.
- Deleting my node_modules folder and package-lock.json and
reinstalling all modules.- Verified that my serverless.yml and webpack.config.js are valid
The weird part is that no matter which modules I put in dependencies I
always get the same result on sls package. Here is the output:Time: 1133ms
Built at: 05/30/2019 2:42:57 PM
Asset Size Chunks Chunk Names
app.js 31.3 KiB 0 [emitted] app
app.js.map 50.3 KiB 0 [emitted] app
Entrypoint app = app.js app.js.map
[1] external "express" 42 bytes {0} [built]
[2] external "passport" 42 bytes {0} [built]
[3] external "bcryptjs" 42 bytes {0} [built]
[5] external "cors" 42 bytes {0} [built]
[6] external "cookie-parser" 42 bytes {0} [built]
[7] external "helmet" 42 bytes {0} [built]
[8] external "serverless-http" 42 bytes {0} [built]
[9] external "passport-jwt" 42 bytes {0} [built]
[14] external "core-js/modules/es7.array.flat-map" 42 bytes {0} [built]
[15] external "core-js/modules/es6.array.sort" 42 bytes {0} [built]
[16] external "core-js/modules/es7.promise.finally" 42 bytes {0} [built]
[17] external "core-js/modules/es7.symbol.async-iterator" 42 bytes {0} [built]
[18] external "core-js/modules/es7.string.trim-left" 42 bytes {0} [built]
[19] external "core-js/modules/es7.string.trim-right" 42 bytes {0} [built]
[20] ./app.js + 13 modules 22.9 KiB {0} [built]
| ./app.js 1.29 KiB [built]
| ./utils/auth/passport.js 529 bytes [built]
| ./routes/users.js 771 bytes [built]
| ./routes/search.js 690 bytes [built]
| ./controllers/users.js 4.81 KiB [built]
| ./controllers/search.js 3.29 KiB [built]
| ./utils/validation/users.js 5.37 KiB [built]
| ./models/users.js 1.47 KiB [built]
| ./utils/search/elasticsearchConfig.js 152 bytes [built]
| ./models/search.js 929 bytes [built]
| ./utils/search/elasticsearch.js 2.3 KiB [built]
| ./utils/validation/isEmpty.js 335 bytes [built]
| ./utils/validation/recaptcha.js 498 bytes [built]
| ./utils/db/mongodb.js 476 bytes [built]
+ 6 hidden modules—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/serverless-heaven/serverless-webpack/issues/43?email_source=notifications&email_token=ABKEZXSTZ6QOXJ5KH3RK7NTPYAOWBA5CNFSM4CSON7OKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWTF5FA#issuecomment-497442452,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABKEZXWJ7GQ6R77HWHQ5NPLPYAOWBANCNFSM4CSON7OA
.
The related issue is https://github.com/serverless-heaven/serverless-webpack/issues/505
I faced a very similar problem in combination with apollo-server-lambda / @apollo/gateway.
The execution failed with the following error:
Unable to import module 'handler': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous>***(/var/task/node_modules/@apollo/gateway/dist/loadServicesFromRemoteEndpoint.js:13:22)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
Thanks to the hint that missing deps could be the cause of those kinds of errors, I was able to fix it by taking a closer look at the error message. On line 7 (*) you can see where exactly the error originated. On line 13 of the mentioned file (loadServicesFromRemoteEndpoint.js), a module was imported:
const node_fetch_1 = require("node-fetch");
When running serverless package this dependency was indeed missing in the node_modules folder
of the create zip file.
The solution / workaround
Just install the missing dependecy and require it in the beginning of your handler file.
import 'node-fetch';
This may be very specific to this rather exotic combination of federated GraphQL schemas and AWS Lambda, but I hope it can help somebody anyways, as this error may happen if any of your dependencies is missing a sub-dependency it is using in the package.json, as it seems to be the case with @apollo/gateway.
Most helpful comment
In my case, making sure that ALL required modules were actually in package.json solved the issue :)