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)

Most upvoted comments

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-291845122

And 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.

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.

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 run --privileged -v /cygdrive/c/Users/charles.campbell/workspace/uni-api:/var/task lambci/lambda:nodejs6.10 index.handler
START RequestId: f8f7a1e3-643c-1f43-bb6a-03d525c083f9 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: f8f7a1e3-643c-1f43-bb6a-03d525c083f9
REPORT RequestId: f8f7a1e3-643c-1f43-bb6a-03d525c083f9  Duration: 8.51 ms       Billed Duration: 100 ms Memory Size: 1536 MB    Max Memory Used: 28 MB

{"errorMessage":"Cannot find module '/var/task/index'","errorType":"Error","stackTrace":["Function.Module._load (module.js:417:25)","Module.require (module.js:497:17)","require (internal/module.js:20:19)"]}

Docker versions:

$ docker version
Client:
 Version:      17.09.1-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   19e2cf6
 Built:        Thu Dec  7 22:22:26 2017
 OS/Arch:      windows/amd64

Server:
 Version:      17.09.1-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   19e2cf6
 Built:        Thu Dec  7 22:28:28 2017
 OS/Arch:      linux/amd64
 Experimental: false

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:

docker run --privileged -v /var/www/uni-api:/var/task lambci/lambda:nodejs6.10 index.handler
START RequestId: 87e11c12-2b54-1b5b-8ab2-32e1ceb0b3e8 Version: $LATEST
END RequestId: 87e11c12-2b54-1b5b-8ab2-32e1ceb0b3e8
REPORT RequestId: 87e11c12-2b54-1b5b-8ab2-32e1ceb0b3e8  Duration: 133.69 ms     Billed Duration: 200 ms Memory Size: 1536 MB    Max Memory Used: 36 MB

{"statusCode":200,"body":"{\"Output\":\"Hello World!\"}","headers":{"x-powered-by":"Express","content-type":"application/json; charset=utf-8","content-length":"25","etag":"W/\"19-4rINYYbMk9K1H37xC41nYMjaDpg\"","date":"Wed, 13 Dec 2017 20:54:39 GMT","connection":"close"},"isBase64Encoded":false}

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.

The root problem seems to be SELinux.

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 make aws-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.