aws-sam-cli: Invoking sam local invoke returns an Unable to import module 'index': Error
I’m trying to play with the Hello-world example located in the samples of the repository. However upon invoking sam local invoke "HelloWorld" -e event.json
inside the directory where the index, template.yml and is located.
it automatically returns the ff:
START RequestId: e50a858c-69d8-16fc-02d3-70199542bc48 Version: $LATEST Unable to import module 'index': 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) END RequestId: e50a858c-69d8-16fc-02d3-70199542bc48 REPORT RequestId: e50a858c-69d8-16fc-02d3-70199542bc48 Duration: 31.25 ms Billed Duration: 0 ms Memory Size: 0 M
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 5
- Comments: 32 (2 by maintainers)
FWIW I recently started having this issue trying to test a function that was mounting fine previously. I reset docker to factory defaults, and when I next ran sam local was prompted to allow the drive to be shared and had to enter my password, now it’s working. Quite possibly caused by my password having changed since I allowed the drive share on docker.
Maybe I’m on the different case but I’ve resolved the similar
Unable to import module
issue on Windows, I hope sharing my workaround helps.I’m using the Windows subsystem for Linux but running Docker on Windows and using Docker on Linux by mapping the port. On WSL,
/mnt/c/
is mounted to Windows file system by default.The problem was that this
/mnt/c/blahblah
paths are not mounted when running a docker container on WSL, checking on this kind of commands:docker run -it -v /var/run/docker.sock:/var/run/docker.sock -v /mnt/c/Documents/current-working-directory:/data alpine
So I re-mounted the
C:\
to/c/
on Linux like this comment: https://github.com/Microsoft/WSL/issues/1854#issuecomment-291845122And I’ve invoked my function like this:
sam local invoke --docker-volume-basedir $(pwd -P) "Hello"
And it worked!
This can also happen when you have a complication error in index.js. A bad import or perhaps not installing a node module necessary for index.js. The error message acts as if it can’t find it, but in reality, it is because complication failed.
Instead of resetting Docker to factory defaults you can select “Reset credentials” under the “Shared Drives” tab in Docker Settings. That should force a re-authentication using your current Windows user/pass. This resolved my problem.
@zhabba, I’m have the same issue. I’m running on Windows 10 with Cygwin.
Docker versions:
NPM Version: 3.10.10 Sam Version: sam version 0.2.4 AWS Version: aws-cli/1.14.6 Python/2.7.9 Windows/8 botocore/1.8.10
Running this same code on a Linux server works with or without
--privileged
:It looks like it might be a folder permissions issue but I’m stumped. I was following along with this tutorial if that helps any.
@Stubie17 thanks. This fixed my issue on Windows as well. To elaborate, instead of completely resetting Docker, you can optionally go to Settings > Shared Drives and un-check your drive. Once you run
sam local start-api
again, it will prompt you to allow access to your drive and ask for username and password.To expand upon this, if docker is set to run with the --selinux-enabled flag in /etc/sysconfig/docker, the (lack of) SELinux labeling on the host directory being passed to the container as a volume will cause the underlying docker container to be unable to read the files.
You can address this without disabling SELinux protection in Docker, however! To work around this issue on SELinux-enabled systems, tag your lambda’s working directory on the docker host with the svirt_sandbox_file_t tag:
chcon -Rt svirt_sandbox_file_t .
This should makeaws-sam-local start-api
work.I had the same problem: We want to include a self-made npm module. We are using typescript and did not bundle everything to a single index.js or index.ts file within the dependent module.
I just played around and after moving a class within the module to index.ts, publish it, run npm install and then sam local again, everything works fine.
Well this is not a general insight or a general solution, but it seems (for our use-case at least) this would solve the problem: bind every typescript class and interface to a single index.js during compile time before publishing the npm module.
@francistec first, if you’re running command under the Windows change “$PWD” to actual absolute path to directory with your code, like: C:\what\ever\aws-sam-local\samples\hello-world\node. And second, pass JSON as a string: “{"name":"Bob"}”.
The root problem seems to be SELinux. I tried to run lambci/lambda:nodejs6.10 container directly in example dir as it described at lambci repo:
docker run -v "$PWD":/var/task lambci/lambda:nodejs6.10 index.handler {\"name\":\"Bob\"}
and it failed and then I tried it with--privileged
option:docker run --privileged -v "$PWD":/var/task lambci/lambda:nodejs6.10 index.handler {\"name\":\"Bob\"}
and execution succeeded.