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...
package.json
file directly will break npm. npm tries to rename the file on save, which is not possible when its a mounted file./data
) will mount the node_modules folder./data
, example /dist
package.json
in /data/conf
mounting the /data/conf
as a volume instead wont work. I cant find any way to specify the package.json
path in npmrc
.package.json
in /data/conf
and symlinking it to /data/package.json
wont work. npm breaks the symlink and replaces it with a file.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..
To avoid the npm install phase on every docker build just copy those lines and change the ^/opt/app^ to the location your app lives inside the container. That works.
Description. This command installs a package and any packages that it depends on. If the package has a package-lock, or an npm shrinkwrap file, or a yarn lock file, the installation of dependencies will be driven by that, respecting the following order of precedence: npm-shrinkwrap. json.
npm is the command-line interface to the npm ecosystem. It is battle-tested, surprisingly flexible, and used by hundreds of thousands of JavaScript developers every day. Docker can be classified as a tool in the "Virtual Machine Platforms & Containers" category, while npm is grouped under "Front End Package Manager".
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..
A similar and alternative way is to use NODE_ENV variable instead of creating a symlink.
RUN mkdir -p /dist/node_modules
RUN cp -r node_modules/* /dist/node_modules/
ENV NODE_PATH /dist/node_modules
Here you first create a new directory for node_modules, copy all modules there, and have Node read the modules from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With