Accessing node_modules after npm install inside Do

2020-07-23 04:01发布

问题:

I am running Docker with Docker Machine on Mac. I successfully set up some containers and ran npm install inside them, as explained here. This installs the node_modules inside the image and inside the container, but they are not available on the host, i.e. my IDE complains about missing node_modules.

Am I missing something? What is the best way to run npm install inside the container but be able to do development (with these dependencies) on the host?

From my docker-compose.yml:

  volumes:
    - /Users/andre/IdeaProjects/app:/home/app
    - /home/app/node_modules

回答1:

Since you are using boot2docker, only Max host folder /Users/ is mounted and accessible from the boot2docker host.

That means you would need to map /home/app/node_modules to a Mac host path starting with /Users, to see said modules on your Mac host.

volumes:
    - /Users/andre/IdeaProjects/app:/home/app
    - /Users/andre/node_modules:/home/app/node_modules


回答2:

You will not be able to access on your host to your node_modules folder inside the container. It is not recommended to bind this folder to your host folder node_modules cause It will cause problems while building the images.

One cheap solution will be to use the Visual Studio Code Extension called "Remote-Containers". This extension will allow you to attach your Visual Studio Code to your container an edit transparently files within your container folders. To do so, it will install an internal vscode server within your development container. For more information check this link.

Ensure, however, that your volumes still are created in your docker-compose.yml file.

I hope it helps :D!