Unable to import Module in Lambda

Viewed 3697

I have a simple exports.js file and I have zipped the folder and upload it to Lambda but on run time it gives an error:

"errorMessage": "Cannot find module 'exports'",
"errorType": "Error",
"stackTrace": 
[
"Function.Module._resolveFilename (module.js:338:15)",
"Function.Module._load (module.js:280:25)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)"
]

Any help would be appreciated. Thanks

3 Answers

Include a directory called node_modules in your directory. Zip the your index.js + the node_modules directory and upload.

I have similar experience. While what I did is in the serverless but I think the error still relevant because it's also comes from AWS lambda. This is the error that I saw :

Unable to import module 'src/handlers/list': Error
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/task/src/handlers/list.js:400:18)
    at __webpack_require__ (/var/task/src/handlers/list.js:20:30)
    at Object.<anonymous> (/var/task/src/handlers/list.js:370:18)
    at __webpack_require__ (/var/task/src/handlers/list.js:20:30)
    at /var/task/src/handlers/list.js:63:18
    at Object.<anonymous> (/var/task/src/handlers/list.js:66:10)

I am using the webpack and I solved it by removing a library in my webpack.config.js and the library name is self-explanatory.

what I did is removing this line from webpack.config.js :

const nodeExternals = require("webpack-node-externals");

please let me know if you need more details. Thank you

Related