New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoking sam local invoke returns an Unable to import module 'index': Error #208

Closed
kyeljmd opened this Issue Dec 1, 2017 · 27 comments

Comments

Projects
None yet
@kyeljmd

kyeljmd commented Dec 1, 2017

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

@zhabba

This comment has been minimized.

zhabba commented Dec 2, 2017

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.

@kyeljmd

This comment has been minimized.

kyeljmd commented Dec 2, 2017

@zhabba I'm invoking the said function on a windows machine is there anyway I can identify where the container is located directly by default in a windows machine? so I can run the commend you have specified? I've tried running your command in the same dir where the files are located

docker run --privileged -v "$PWD":/var/task lambci/lambda:nodejs6.10 index.handler {\"name\":\"Bob\"}

and it resulted to this

docker: Error response from daemon: create $PWD: "$PWD" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path. See 'docker run --help'

I am running on a windows 10 machine

@zhabba

This comment has been minimized.

zhabba commented Dec 2, 2017

@kyeljmd please change "$PWD" to actual path where you're running container as error suggests: If you intended to pass a host directory, use absolute path. $PWD is for Linux :)

@francistec

This comment has been minimized.

francistec commented Dec 9, 2017

Any Solution. I have the same issue.

Error Message:
{"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)"]}
I trie with this commands:

  • sam local invoke HelloWorld -e event.json
  • docker run --privileged -v "$PWD":/var/task lambci/lambda:nodejs6.10 index.handler {"name":"Bob"}

I'm tried to run HelloWorld sample.

@zhabba

This comment has been minimized.

zhabba commented Dec 9, 2017

@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"}".

@ctcampb3

This comment has been minimized.

ctcampb3 commented Dec 13, 2017

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

@sk-saurabh

This comment has been minimized.

sk-saurabh commented Dec 24, 2017

Did it work for anyone on Windows?
I am encountering the same error while running HelloWorld sample. I am running on Windows7 using Docker ToolBox.
Here is the error:
START RequestId: 0b314379-924e-174f-0a90-30d8ccf4d7ae 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: 0b314379-924e-174f-0a90-30d8ccf4d7ae
REPORT RequestId: 0b314379-924e-174f-0a90-30d8ccf4d7ae Duration: 13.49 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)"]}

@universam1

This comment has been minimized.

universam1 commented Dec 29, 2017

same here

@Kivol

This comment has been minimized.

Kivol commented Jan 14, 2018

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:
Microsoft/WSL#1854 (comment)

And I've invoked my function like this:
sam local invoke --docker-volume-basedir $(pwd -P) "Hello"

And it worked!

@JonathanJonathanJonathan

This comment has been minimized.

JonathanJonathanJonathan commented Jan 15, 2018

Same problem here with the hello-world sample, Unable to mount the volume... I tried all of the above solutions, none of them worked. Using windows 10.

@Stubie17

This comment has been minimized.

Stubie17 commented Jan 15, 2018

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.

@JonathanJonathanJonathan

This comment has been minimized.

JonathanJonathanJonathan commented Jan 15, 2018

@Stubie17 That resolved my problems with the HelloWorld sample, thanks.

@bjxuch

This comment has been minimized.

bjxuch commented Jan 16, 2018

Same issue, my config is:
1.win8.1
2.SAM 0,2,4
3.Docker version 17.10.0-ce, build f4ffd25

@mhluska

This comment has been minimized.

mhluska commented Jan 18, 2018

@JonathanJonathanJonathan @Stubie17 factory default reset didn't work for me.

@marcesher

This comment has been minimized.

marcesher commented Jan 20, 2018

@Kivol Thank you! I was banging my head against the wall yesterday on this with WSL. So glad you posted your solution. 🙏

@justme8910

This comment has been minimized.

justme8910 commented Jan 30, 2018

For everyone using Windows 7 and Docker Toolbox (Docker version 18.01.0-ce, build 03596f51b1):

  • Make sure that the working directory is mounted in Virtual Box for your docker host (Settings -> Shared Folders) e.g. mount C:/test/helloworld on /var/task. Now the folder is available on the host machine
  • Run docker run -v /var/task:/var/task lambci/lambda::nodejs6.10 index.handler
    This mounts the folder from the host in the docker container.

You can also use
sam local invoke -v //var/task -e event.json "HelloWorldFunction"

@purefan

This comment has been minimized.

Contributor

purefan commented Feb 1, 2018

FWIW @zhabba 's suggestion to use absolute paths fixed it for me

@psogani

This comment has been minimized.

psogani commented Feb 9, 2018

Hi i am seeing the same error while running the command "sam local invoke "HelloWorld" -e input/events.json " from the directory where my index.js and template.yml are located on an amazon linux EC2 instance.

[uvb125@ip-10-205-233-250 uvb125_serverless_test]$ dzdo sam local invoke "HelloWorld" -e input/events.json 
2018/02/09 11:34:20 Successfully parsed template.yml
2018/02/09 11:34:20 Connected to Docker 1.32
2018/02/09 11:34:20 Fetching lambci/lambda:nodejs6.10 image for nodejs6.10 runtime...
nodejs6.10: Pulling from lambci/lambda
Digest: sha256:b72789e8a544e658a76d71a0abbb2fa8b4122cc882bcd27097037bd28f657c9b
Status: Image is up to date for lambci/lambda:nodejs6.10
2018/02/09 11:34:21 Invoking index.handler (nodejs6.10)
2018/02/09 11:34:21 WARNING: No AWS credentials found. Missing credentials may lead to slow startup times as detailed in https://github.com/awslabs/aws-sam-local/issues/134
2018/02/09 11:34:21 Mounting /tmp/uvb125_serverless_test as /var/task:ro inside runtime container
START RequestId: 2ea3aae5-1457-1fc5-2725-22c799036905 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: 2ea3aae5-1457-1fc5-2725-22c799036905
REPORT RequestId: 2ea3aae5-1457-1fc5-2725-22c799036905	Duration: 7.87 ms	Billed Duration: 100 ms	Memory Size: 128 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)"]}

I would greatly appreciate any help towards resolving this. Thanks in advance!

@jkeczan

This comment has been minimized.

jkeczan commented Feb 12, 2018

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.

@subtonez

This comment has been minimized.

subtonez commented Feb 15, 2018

@Stubie17 Thanks!! Also Windows 10. I just had to reset my credentials for the shared drive.

@arshadcomp

This comment has been minimized.

arshadcomp commented Mar 17, 2018

Hi I am using Ubuntu-14.04. Using Docker version

Client:
 Version:      1.12.6-cs13
 API version:  1.23
 Go version:   go1.6.4
 Git commit:   0ee24d4
 Built:        Mon Jul 24 18:09:19 2017
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.6-cs13
 API version:  1.24
 Go version:   go1.6.4
 Git commit:   0ee24d4
 Built:        Mon Jul 24 18:09:19 2017
 OS/Arch:      linux/amd64

SAM Local sam version 0.2.8

I am trying to run the HelloWorld template and getting the following error:

:~/Documents/SAM/hello_world$ sam local invoke -e even.json HelloWorld
2018/03/17 13:21:46 Successfully parsed template.yaml
2018/03/17 13:21:46 Connected to Docker 
2018/03/17 13:21:46 Fetching lambci/lambda:nodejs6.10 image for nodejs6.10 runtime...
nodejs6.10: Pulling from lambci/lambda
Digest: sha256:bc47c223a2accf47c51adc6e1949bda9b557764be38a76ebac73e545044079cc
Status: Image is up to date for lambci/lambda:nodejs6.10
2018/03/17 13:21:50 Invoking index.js (nodejs6.10)
2018/03/17 13:21:50 Mounting /home/arshad/Documents/SAM/hello_world as /var/task:ro inside runtime container
START RequestId: 74bc559a-03e1-19e9-a399-1a84ed133013 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: 74bc559a-03e1-19e9-a399-1a84ed133013
REPORT RequestId: 74bc559a-03e1-19e9-a399-1a84ed133013	Duration: 6.65 ms	Billed Duration: 100 ms	Memory Size: 128 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)"]}

Even with docker command:
docker run --rm -v "$PWD":/var/task lambci/lambda:nodejs6.10 index.js '{"name": "Bob'}'
I am getting the error:

START RequestId: ea343f22-a394-1da2-cc3f-1e2c0fdb2656 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: ea343f22-a394-1da2-cc3f-1e2c0fdb2656
REPORT RequestId: ea343f22-a394-1da2-cc3f-1e2c0fdb2656	Duration: 7.30 ms	Billed Duration: 100 ms	Memory Size: 1536 MB	Max Memory Used: 28 MB

I am getting nowhere with the solutions above. Please help.

@goosefraba

This comment has been minimized.

goosefraba commented Mar 19, 2018

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.

@jmthomas-mitre

This comment has been minimized.

jmthomas-mitre commented Mar 20, 2018

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.

@E-G-C

This comment has been minimized.

E-G-C commented Apr 6, 2018

I just have the same problem running the python "hello world" sample on a brand new win 10 machine. the quickest fix based on based on @Kivol comments was to remap my c drive in VirtualBox
image

@mariotacke

This comment has been minimized.

mariotacke commented Apr 27, 2018

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

@sanathkr

This comment has been minimized.

Contributor

sanathkr commented Jun 1, 2018

Thanks everyone for the instructions to fix this issue. It is not a SAM CLI bug but a mis-configuration in the Docker sharing settings. Hence closing the issue

@sanathkr sanathkr closed this Jun 1, 2018

@qm3ster

This comment has been minimized.

qm3ster commented Jul 29, 2018

doing chcon -Rt svirt_sandbox_file_t . as per @jmthomas-mitre's suggestion prevents my user from editing the files.
How can I both allow docker read access and maintain my user's write access?
My setup:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment