4

I'm attempting to use Docker Compose to bring together a number of Node.js apps in my development environment. I'm running into an issue, however, with node_modules.

Here's what's happening:

  1. npm install is run as part of the Dockerfile.
  2. I do not have node_modules in my local directory. (I shouldn't because the installation of dependencies should happen in the container, right? It seems to defeat the purpose otherwise, since I'd need to have Node.js installed locally.)
  3. In docker-compose.yml, I'm setting up a volume with the source code.
  4. docker-compose build runs fine.
  5. When I docker-compose up, the node_modules directory disappears in the container — I'm assuming because the volume is mounted and I don't have it in my local directory.

How do I ensure that node_modules sticks around?

Dockerfile

FROM       node:0.10.37

COPY       package.json /src/package.json

WORKDIR    /src

RUN        npm install -g grunt-cli && npm install

COPY       . /src

EXPOSE     9001

CMD        ["npm", "start"]

docker-compose.yml

api:
  build: .
  command: grunt
  links:
    - elasticsearch
  ports:
    - "9002:9002"
  volumes:
    - .:/src

elasticsearch:
  image: elasticsearch:1.5
3

2 Answers 2

3

Due to the way Node.js loads modules, simply place node_modules higher in the source code path. For example, put your source at /app/src and your package.json in /app, so /app/node_modules is where they're installed.

Sign up to request clarification or add additional context in comments.

Comments

0

I tried your fix, but the issue is that most people run npm install in the /usr/src/app directory, resulting in the node_modules folder ending up in the /app directory. As a result the node_modules folder ends up in both the /usr/src and /usr/src/app directory in the container and you end up with the same issue you started with.

1 Comment

Yes, I ended up running into this as well. I tried really hard to keep everything in the container (including the development flow, like build tools), but I don't think it's feasible realistically. I now run the development flow back on my host machine, so I do need node_modules, which means it's synced to /app/src as you said. So far it's not a problem, but I can see it being one if something is built on my host machine (OS X) that's not compatible with the container (Ubuntu).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.