Docker hot reloading node app with nodemon using Windows 10











up vote
0
down vote

favorite












I have an express app and I'm trying to setup hot reloading with nodemon using docker for windows 10 as a dev environment. However when I npm install on the volume it doesn't seem to work.



With powershell I use a volume like so:




docker build -t node-api .



docker run --rm -it -p 8080:8080 -v "${PWD}:/usr/src/app" node-api




Which outputs this error:



[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server/server.js`
internal/modules/cjs/loader.js:605
throw err;
^

Error: Cannot find module 'express'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
at Function.Module._load (internal/modules/cjs/loader.js:529:25)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/usr/src/app/server/server.js:1:79)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
[nodemon] app crashed - waiting for file changes before starting...


If I exclude the -v flag and docker run then it starts with no errors, but doesn't detect changes or restart on file saves.



Dockerfile



FROM node:alpine

#Create app directory
WORKDIR /usr/src/app

#Install nodemon for hot reloading
RUN npm install nodemon -g

#Install app dependencies
#A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install

#Bundle app source
COPY . .

EXPOSE 8080
CMD [ "nodemon", "-L", "server/server.js" ]


Folder Structure



server/

>server.js

Dockerfile

package.json


Github repo for code










share|improve this question
























  • You overwrote the node_modules folder by mounting your whole windows folder into a linux docker container, thus express and other deps are missing. You might want to only mount app/src or change your directory structure to allow that.
    – William Chong
    Nov 21 at 20:27












  • Hey William thanks for your help. I'm still a little confused could you show an example please? I tried mounting at various levels with no success. And I edited my question to include my folder structure - how would you fix it?
    – Brayden Cowell
    Nov 21 at 20:58










  • does docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api work?
    – William Chong
    Nov 21 at 21:02










  • Yes that works! Thanks you so much
    – Brayden Cowell
    Nov 21 at 21:39

















up vote
0
down vote

favorite












I have an express app and I'm trying to setup hot reloading with nodemon using docker for windows 10 as a dev environment. However when I npm install on the volume it doesn't seem to work.



With powershell I use a volume like so:




docker build -t node-api .



docker run --rm -it -p 8080:8080 -v "${PWD}:/usr/src/app" node-api




Which outputs this error:



[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server/server.js`
internal/modules/cjs/loader.js:605
throw err;
^

Error: Cannot find module 'express'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
at Function.Module._load (internal/modules/cjs/loader.js:529:25)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/usr/src/app/server/server.js:1:79)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
[nodemon] app crashed - waiting for file changes before starting...


If I exclude the -v flag and docker run then it starts with no errors, but doesn't detect changes or restart on file saves.



Dockerfile



FROM node:alpine

#Create app directory
WORKDIR /usr/src/app

#Install nodemon for hot reloading
RUN npm install nodemon -g

#Install app dependencies
#A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install

#Bundle app source
COPY . .

EXPOSE 8080
CMD [ "nodemon", "-L", "server/server.js" ]


Folder Structure



server/

>server.js

Dockerfile

package.json


Github repo for code










share|improve this question
























  • You overwrote the node_modules folder by mounting your whole windows folder into a linux docker container, thus express and other deps are missing. You might want to only mount app/src or change your directory structure to allow that.
    – William Chong
    Nov 21 at 20:27












  • Hey William thanks for your help. I'm still a little confused could you show an example please? I tried mounting at various levels with no success. And I edited my question to include my folder structure - how would you fix it?
    – Brayden Cowell
    Nov 21 at 20:58










  • does docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api work?
    – William Chong
    Nov 21 at 21:02










  • Yes that works! Thanks you so much
    – Brayden Cowell
    Nov 21 at 21:39















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an express app and I'm trying to setup hot reloading with nodemon using docker for windows 10 as a dev environment. However when I npm install on the volume it doesn't seem to work.



With powershell I use a volume like so:




docker build -t node-api .



docker run --rm -it -p 8080:8080 -v "${PWD}:/usr/src/app" node-api




Which outputs this error:



[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server/server.js`
internal/modules/cjs/loader.js:605
throw err;
^

Error: Cannot find module 'express'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
at Function.Module._load (internal/modules/cjs/loader.js:529:25)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/usr/src/app/server/server.js:1:79)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
[nodemon] app crashed - waiting for file changes before starting...


If I exclude the -v flag and docker run then it starts with no errors, but doesn't detect changes or restart on file saves.



Dockerfile



FROM node:alpine

#Create app directory
WORKDIR /usr/src/app

#Install nodemon for hot reloading
RUN npm install nodemon -g

#Install app dependencies
#A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install

#Bundle app source
COPY . .

EXPOSE 8080
CMD [ "nodemon", "-L", "server/server.js" ]


Folder Structure



server/

>server.js

Dockerfile

package.json


Github repo for code










share|improve this question















I have an express app and I'm trying to setup hot reloading with nodemon using docker for windows 10 as a dev environment. However when I npm install on the volume it doesn't seem to work.



With powershell I use a volume like so:




docker build -t node-api .



docker run --rm -it -p 8080:8080 -v "${PWD}:/usr/src/app" node-api




Which outputs this error:



[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server/server.js`
internal/modules/cjs/loader.js:605
throw err;
^

Error: Cannot find module 'express'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
at Function.Module._load (internal/modules/cjs/loader.js:529:25)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/usr/src/app/server/server.js:1:79)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
[nodemon] app crashed - waiting for file changes before starting...


If I exclude the -v flag and docker run then it starts with no errors, but doesn't detect changes or restart on file saves.



Dockerfile



FROM node:alpine

#Create app directory
WORKDIR /usr/src/app

#Install nodemon for hot reloading
RUN npm install nodemon -g

#Install app dependencies
#A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install

#Bundle app source
COPY . .

EXPOSE 8080
CMD [ "nodemon", "-L", "server/server.js" ]


Folder Structure



server/

>server.js

Dockerfile

package.json


Github repo for code







node.js windows docker express nodemon






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 20:49

























asked Nov 21 at 20:21









Brayden Cowell

204




204












  • You overwrote the node_modules folder by mounting your whole windows folder into a linux docker container, thus express and other deps are missing. You might want to only mount app/src or change your directory structure to allow that.
    – William Chong
    Nov 21 at 20:27












  • Hey William thanks for your help. I'm still a little confused could you show an example please? I tried mounting at various levels with no success. And I edited my question to include my folder structure - how would you fix it?
    – Brayden Cowell
    Nov 21 at 20:58










  • does docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api work?
    – William Chong
    Nov 21 at 21:02










  • Yes that works! Thanks you so much
    – Brayden Cowell
    Nov 21 at 21:39




















  • You overwrote the node_modules folder by mounting your whole windows folder into a linux docker container, thus express and other deps are missing. You might want to only mount app/src or change your directory structure to allow that.
    – William Chong
    Nov 21 at 20:27












  • Hey William thanks for your help. I'm still a little confused could you show an example please? I tried mounting at various levels with no success. And I edited my question to include my folder structure - how would you fix it?
    – Brayden Cowell
    Nov 21 at 20:58










  • does docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api work?
    – William Chong
    Nov 21 at 21:02










  • Yes that works! Thanks you so much
    – Brayden Cowell
    Nov 21 at 21:39


















You overwrote the node_modules folder by mounting your whole windows folder into a linux docker container, thus express and other deps are missing. You might want to only mount app/src or change your directory structure to allow that.
– William Chong
Nov 21 at 20:27






You overwrote the node_modules folder by mounting your whole windows folder into a linux docker container, thus express and other deps are missing. You might want to only mount app/src or change your directory structure to allow that.
– William Chong
Nov 21 at 20:27














Hey William thanks for your help. I'm still a little confused could you show an example please? I tried mounting at various levels with no success. And I edited my question to include my folder structure - how would you fix it?
– Brayden Cowell
Nov 21 at 20:58




Hey William thanks for your help. I'm still a little confused could you show an example please? I tried mounting at various levels with no success. And I edited my question to include my folder structure - how would you fix it?
– Brayden Cowell
Nov 21 at 20:58












does docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api work?
– William Chong
Nov 21 at 21:02




does docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api work?
– William Chong
Nov 21 at 21:02












Yes that works! Thanks you so much
– Brayden Cowell
Nov 21 at 21:39






Yes that works! Thanks you so much
– Brayden Cowell
Nov 21 at 21:39














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The problem is that by mounting the whole host app/ directory as volume, the app/node_modules/ inside the container would be overwritten by the host's app/, thus all dependencies are missing.



The solution is to only mount the source code folder you need, i.e.



docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419922%2fdocker-hot-reloading-node-app-with-nodemon-using-windows-10%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    The problem is that by mounting the whole host app/ directory as volume, the app/node_modules/ inside the container would be overwritten by the host's app/, thus all dependencies are missing.



    The solution is to only mount the source code folder you need, i.e.



    docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      The problem is that by mounting the whole host app/ directory as volume, the app/node_modules/ inside the container would be overwritten by the host's app/, thus all dependencies are missing.



      The solution is to only mount the source code folder you need, i.e.



      docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        The problem is that by mounting the whole host app/ directory as volume, the app/node_modules/ inside the container would be overwritten by the host's app/, thus all dependencies are missing.



        The solution is to only mount the source code folder you need, i.e.



        docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api





        share|improve this answer












        The problem is that by mounting the whole host app/ directory as volume, the app/node_modules/ inside the container would be overwritten by the host's app/, thus all dependencies are missing.



        The solution is to only mount the source code folder you need, i.e.



        docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 2:27









        William Chong

        826416




        826416






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419922%2fdocker-hot-reloading-node-app-with-nodemon-using-windows-10%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)