node.jsnpmdocker

npm package.json and docker (mounting it...)


I am using Docker, so this case might look weird. But I want my whole /data directory to be mounted inside my docker container when developing.

My /data folder container my package.json file, an app directory and a bunch of other stuff. The problem is that I want my node_modules folder to NOT be persistent, only the package.json file.

I have tried a couple of things, but package.json and npm is giving me a hard time here...

Copying data back and forth to/from inside the docker container is how I am doing it now.. A little tedious.. I also want a clean solution..


Solution

  • As you have already answered, I think that might be the only solution right now.

    When you are building your Docker image, do something like:

    COPY data/package.json /data/
    RUN mkdir /dist/node_modules && ln -s /dist/node_modules /data/node_modules && cd /data && npm install
    

    And for other stuff (like bower, do the same thing)

    COPY data/.bowerrc /data/
    COPY data/bower.json /data/
    RUN mkdir /dist/vendor && ln -s /dist/vendor /data/vendor && cd /data && bower install --allow-root
    

    And COPY data/ /data at the end (so you are able to use Dockers caching and not having to do npm/docker installation when there is a change to data.

    You will also need to create the symlinks you need and store them in your git-repo. They will be invalid on the outside, but will happely work on the inside of your container.

    Using this solution, you are able to mount your $PWD/data:/data without getting the npm/bower "junk" outside your container. And you will still be able to build your image as a standalone deployment of your service..