I am writing a code which requires csv-parse on lambda.
Runtime is node 12.x.
I installed following modules via npm 6.14.10 (node v12.20.1)
{
"name": "data_analysis_node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.1282.0",
"csv-parse": "^5.3.3",
"fs": "0.0.1-security"
}
}
Then I zipped a node_modules directory, upload it, attached it to a layer, applyed the layer to the function and wrote a following code.
index.js
const AWS = require('aws-sdk');
const parse = require('csv-parse/lib/sync');
const fs = require('fs');
exports.handler = async (event) => {
console.log('fine')
};
but it has not been working and showing following errors.
2022-12-26T14:28:45.472Z undefined ERROR Uncaught Exception {
"errorType":"Runtime.ImportModuleError"
,"errorMessage":"Error: Cannot find module 'csv-parse/lib/sync'nRequire
stack:n- /var/task/index.jsn- /var/runtime/UserFunction.jsn- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'csv-parse/lib/sync'","Require stack:","- /var/task/index.js","- /var/runtime/UserFunction.js","-
/var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
I re-checked the inside the node_modules, and I confirmed the path to csv-parse is correct.
I also commented out the line imports csv-parse, and it didn’t output any errors and seemed importing other modules fine.
const AWS = require('aws-sdk');
//const parse = require('csv-parse/lib/sync');
const fs = require('fs');
exports.handler = async (event) => {
console.log('fine')
};
I have been stacked 3 to 4 hours, so any suggestion are helpful.
2
Answers
When creating a Lambda layer for NodeJS, structure in your zip file should be
nodejs/node_modules/
and not justnode_modules/
Optionally, for multiple versions, you can put
nodejs/nodeXY/node_modules/
You can read more about Lambda layers in the documentation
Would something like this work?
It may be that there is a naming collision with the csv-parse API
EDIT
According to the docs you need to require the parse module from
csv-parse/sync
, notcsv-parse/lib/sync
The following should work:
I received the same error you did when requiring from
csv-parse/lib/sync
and it worked fine when required fromcsv-parse/sync