Question:
I am working on a lambda that makes use of modules (async, request, etc)
1 2 3 4 5 6 7 8 9 10 11 12 |
Unable to import module 'index': Error at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at Object. at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.require (module.js:364:17) |
Sample code:
1 2 3 4 5 6 7 8 9 10 |
var AWS = require('aws-sdk'), util = require('util'), request = require('request'); exports.handler = function(event, context) { console.log('test'); context.done(); }; |
It works fine (prints test) as long as no 3rd party modules (besides aws-sdk) are required. As soon as I just add a line such as:
1 2 |
require('request') // or async, config and so on |
It fails with the above error. I have tried calling these modules directly as well by specifying the full path with no luck. It’s like its looking at the wrong directory when calling require
.
Dumping process.env
in the console yields:
1 2 3 4 5 6 7 8 9 10 11 12 |
PATH: '/usr/local/bin:/usr/bin:/bin', LAMBDA_TASK_ROOT: '/var/task', LAMBDA_RUNTIME_DIR: '/var/runtime', AWS_REGION: 'us-west-2', AWS_DEFAULT_REGION: 'us-west-2', AWS_LAMBDA_LOG_GROUP_NAME: '/aws/lambda/Thumbnailer', AWS_LAMBDA_LOG_STREAM_NAME: '2015/12/10/[$LATEST]3f8ef236195448c88f206634bde6301b', AWS_LAMBDA_FUNCTION_NAME: 'Thumbnailer', AWS_LAMBDA_FUNCTION_MEMORY_SIZE: '512', AWS_LAMBDA_FUNCTION_VERSION: '$LATEST', NODE_PATH: '/var/runtime:/var/task:/var/runtime/node_modules', |
Here’s the module I was working off – evidently this used to work at some point but does not for me.
Ideas? I feel that I am missing some configuration unique to lambdas here.
Answer:
omg that was painful… Turns out that OSX makes the node_modules
folder readable only by the user and AWS cannot read it. Make the node_modules
folder and content readable by world and it works. I am not sure if all OSX setups react the same. I am using nvm
which may be the culprit.
Update. I used to set all files to 0666
but ran into issues with executables. Here’s a little script that will address things properly. It will set all files to 0666
unless executables or a directory in which case 0777
. Run this from within the project folder (do be careful of the implications of not doing so!):
Here’s a script from a question I posted:
1 2 3 4 5 |
#!/bin/bash find . \ '(' -perm -0700 -exec chmod 0777 '{}' + ')' -o \ '(' -perm -0600 -exec chmod 0666 '{}' + ')' |