Local nodejs module not being found by docker












0















I have a nodejs module called my-common that contains a couple of js files. These js files export functions that are used throughout a lot of other modules.



My other module (called demo) contains a dependency to the the common module like this:



"dependencies": {
"my-common": "file:../my-common/",
}


When I goto the demo directory and run npm start it works fine. I then build a docker image using the following Dockerfile:



FROM node:8
ENV NODE_ENV=production
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]



When I start the image I get an error that my-common can not be found. I'm guessing that the my-common module isn't being copied into the node_modules directory of the demo module.



I have tried npm link however I think it's a really really really bad idea to need sudo permission to install a global module because this could cause problems on other systems.



I have tried npm install my-common/ in the root directory and that installs the module into my HOME_DIR/node_modules however that isn't installed either into the docker container.



Everywhere I look there doesn't seem an answer to this very simple question. How can I fix this?










share|improve this question



























    0















    I have a nodejs module called my-common that contains a couple of js files. These js files export functions that are used throughout a lot of other modules.



    My other module (called demo) contains a dependency to the the common module like this:



    "dependencies": {
    "my-common": "file:../my-common/",
    }


    When I goto the demo directory and run npm start it works fine. I then build a docker image using the following Dockerfile:



    FROM node:8
    ENV NODE_ENV=production
    WORKDIR /usr/src/app
    COPY package*.json ./
    RUN npm install --only=production
    COPY . .
    EXPOSE 3000
    CMD [ "npm", "start" ]



    When I start the image I get an error that my-common can not be found. I'm guessing that the my-common module isn't being copied into the node_modules directory of the demo module.



    I have tried npm link however I think it's a really really really bad idea to need sudo permission to install a global module because this could cause problems on other systems.



    I have tried npm install my-common/ in the root directory and that installs the module into my HOME_DIR/node_modules however that isn't installed either into the docker container.



    Everywhere I look there doesn't seem an answer to this very simple question. How can I fix this?










    share|improve this question

























      0












      0








      0








      I have a nodejs module called my-common that contains a couple of js files. These js files export functions that are used throughout a lot of other modules.



      My other module (called demo) contains a dependency to the the common module like this:



      "dependencies": {
      "my-common": "file:../my-common/",
      }


      When I goto the demo directory and run npm start it works fine. I then build a docker image using the following Dockerfile:



      FROM node:8
      ENV NODE_ENV=production
      WORKDIR /usr/src/app
      COPY package*.json ./
      RUN npm install --only=production
      COPY . .
      EXPOSE 3000
      CMD [ "npm", "start" ]



      When I start the image I get an error that my-common can not be found. I'm guessing that the my-common module isn't being copied into the node_modules directory of the demo module.



      I have tried npm link however I think it's a really really really bad idea to need sudo permission to install a global module because this could cause problems on other systems.



      I have tried npm install my-common/ in the root directory and that installs the module into my HOME_DIR/node_modules however that isn't installed either into the docker container.



      Everywhere I look there doesn't seem an answer to this very simple question. How can I fix this?










      share|improve this question














      I have a nodejs module called my-common that contains a couple of js files. These js files export functions that are used throughout a lot of other modules.



      My other module (called demo) contains a dependency to the the common module like this:



      "dependencies": {
      "my-common": "file:../my-common/",
      }


      When I goto the demo directory and run npm start it works fine. I then build a docker image using the following Dockerfile:



      FROM node:8
      ENV NODE_ENV=production
      WORKDIR /usr/src/app
      COPY package*.json ./
      RUN npm install --only=production
      COPY . .
      EXPOSE 3000
      CMD [ "npm", "start" ]



      When I start the image I get an error that my-common can not be found. I'm guessing that the my-common module isn't being copied into the node_modules directory of the demo module.



      I have tried npm link however I think it's a really really really bad idea to need sudo permission to install a global module because this could cause problems on other systems.



      I have tried npm install my-common/ in the root directory and that installs the module into my HOME_DIR/node_modules however that isn't installed either into the docker container.



      Everywhere I look there doesn't seem an answer to this very simple question. How can I fix this?







      node.js docker npm






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 17:36









      Martijn HiemstraMartijn Hiemstra

      112211




      112211
























          1 Answer
          1






          active

          oldest

          votes


















          2














          So, I see a couple different things.



          When Docker runs npm install --only=production in the image, Docker sees file:../my-common/ and looks at the parent directory of the WORKDIR of the Docker image, which is /usr/src/app. Since nothing besides package.json has been copied into the image at that point, it can't find the module. If you want to install everything locally and then move it into the image, you can do that by removing the npm install --only=production command from the Dockerfile, and make sure your .dockerignore file doesn't ignore the node_modules directory.



          If you want to install modules in the image, you need to specifically copy the my-common directory into the docker image. However, Docker doesn't allow you to copy something from a parent directory into a image. Any local content has to be in the context of the Dockerfile. You have a couple options:



          Option 1: Move my-common/ into the root of your project, update your Dockerfile to copy that folder and update package.json to point to the correct location.



          Dockerfile:



          FROM node:8
          ENV NODE_ENV=production
          WORKDIR /usr/src/app
          COPY my-common/ ./
          COPY package*.json ./
          RUN npm install --only=production
          COPY . .
          EXPOSE 3000
          CMD [ "npm", "start" ]


          package.json:



          "dependencies": {
          "my-common": "file:./my-common/",
          }


          Option 2: Move the context of the Docker image up one directory. By this I mean move the Dockerfile to the same level as my-common directory and update your Dockerfile and package.json to reflect that change.



          Dockerfile:



          FROM node:8
          ENV NODE_ENV=production
          WORKDIR /usr/src/app
          RUN mkdir my-common
          COPY ./my-common ./my-common
          COPY ./<projectName>/package*.json .
          RUN npm install --only=production
          COPY ./<projectName> .
          EXPOSE 3000
          CMD [ "npm", "start" ]


          package.json:



          "dependencies": {
          "my-common": "file:./my-common/",
          }





          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',
            autoActivateHeartbeat: false,
            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%2f53379945%2flocal-nodejs-module-not-being-found-by-docker%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









            2














            So, I see a couple different things.



            When Docker runs npm install --only=production in the image, Docker sees file:../my-common/ and looks at the parent directory of the WORKDIR of the Docker image, which is /usr/src/app. Since nothing besides package.json has been copied into the image at that point, it can't find the module. If you want to install everything locally and then move it into the image, you can do that by removing the npm install --only=production command from the Dockerfile, and make sure your .dockerignore file doesn't ignore the node_modules directory.



            If you want to install modules in the image, you need to specifically copy the my-common directory into the docker image. However, Docker doesn't allow you to copy something from a parent directory into a image. Any local content has to be in the context of the Dockerfile. You have a couple options:



            Option 1: Move my-common/ into the root of your project, update your Dockerfile to copy that folder and update package.json to point to the correct location.



            Dockerfile:



            FROM node:8
            ENV NODE_ENV=production
            WORKDIR /usr/src/app
            COPY my-common/ ./
            COPY package*.json ./
            RUN npm install --only=production
            COPY . .
            EXPOSE 3000
            CMD [ "npm", "start" ]


            package.json:



            "dependencies": {
            "my-common": "file:./my-common/",
            }


            Option 2: Move the context of the Docker image up one directory. By this I mean move the Dockerfile to the same level as my-common directory and update your Dockerfile and package.json to reflect that change.



            Dockerfile:



            FROM node:8
            ENV NODE_ENV=production
            WORKDIR /usr/src/app
            RUN mkdir my-common
            COPY ./my-common ./my-common
            COPY ./<projectName>/package*.json .
            RUN npm install --only=production
            COPY ./<projectName> .
            EXPOSE 3000
            CMD [ "npm", "start" ]


            package.json:



            "dependencies": {
            "my-common": "file:./my-common/",
            }





            share|improve this answer




























              2














              So, I see a couple different things.



              When Docker runs npm install --only=production in the image, Docker sees file:../my-common/ and looks at the parent directory of the WORKDIR of the Docker image, which is /usr/src/app. Since nothing besides package.json has been copied into the image at that point, it can't find the module. If you want to install everything locally and then move it into the image, you can do that by removing the npm install --only=production command from the Dockerfile, and make sure your .dockerignore file doesn't ignore the node_modules directory.



              If you want to install modules in the image, you need to specifically copy the my-common directory into the docker image. However, Docker doesn't allow you to copy something from a parent directory into a image. Any local content has to be in the context of the Dockerfile. You have a couple options:



              Option 1: Move my-common/ into the root of your project, update your Dockerfile to copy that folder and update package.json to point to the correct location.



              Dockerfile:



              FROM node:8
              ENV NODE_ENV=production
              WORKDIR /usr/src/app
              COPY my-common/ ./
              COPY package*.json ./
              RUN npm install --only=production
              COPY . .
              EXPOSE 3000
              CMD [ "npm", "start" ]


              package.json:



              "dependencies": {
              "my-common": "file:./my-common/",
              }


              Option 2: Move the context of the Docker image up one directory. By this I mean move the Dockerfile to the same level as my-common directory and update your Dockerfile and package.json to reflect that change.



              Dockerfile:



              FROM node:8
              ENV NODE_ENV=production
              WORKDIR /usr/src/app
              RUN mkdir my-common
              COPY ./my-common ./my-common
              COPY ./<projectName>/package*.json .
              RUN npm install --only=production
              COPY ./<projectName> .
              EXPOSE 3000
              CMD [ "npm", "start" ]


              package.json:



              "dependencies": {
              "my-common": "file:./my-common/",
              }





              share|improve this answer


























                2












                2








                2







                So, I see a couple different things.



                When Docker runs npm install --only=production in the image, Docker sees file:../my-common/ and looks at the parent directory of the WORKDIR of the Docker image, which is /usr/src/app. Since nothing besides package.json has been copied into the image at that point, it can't find the module. If you want to install everything locally and then move it into the image, you can do that by removing the npm install --only=production command from the Dockerfile, and make sure your .dockerignore file doesn't ignore the node_modules directory.



                If you want to install modules in the image, you need to specifically copy the my-common directory into the docker image. However, Docker doesn't allow you to copy something from a parent directory into a image. Any local content has to be in the context of the Dockerfile. You have a couple options:



                Option 1: Move my-common/ into the root of your project, update your Dockerfile to copy that folder and update package.json to point to the correct location.



                Dockerfile:



                FROM node:8
                ENV NODE_ENV=production
                WORKDIR /usr/src/app
                COPY my-common/ ./
                COPY package*.json ./
                RUN npm install --only=production
                COPY . .
                EXPOSE 3000
                CMD [ "npm", "start" ]


                package.json:



                "dependencies": {
                "my-common": "file:./my-common/",
                }


                Option 2: Move the context of the Docker image up one directory. By this I mean move the Dockerfile to the same level as my-common directory and update your Dockerfile and package.json to reflect that change.



                Dockerfile:



                FROM node:8
                ENV NODE_ENV=production
                WORKDIR /usr/src/app
                RUN mkdir my-common
                COPY ./my-common ./my-common
                COPY ./<projectName>/package*.json .
                RUN npm install --only=production
                COPY ./<projectName> .
                EXPOSE 3000
                CMD [ "npm", "start" ]


                package.json:



                "dependencies": {
                "my-common": "file:./my-common/",
                }





                share|improve this answer













                So, I see a couple different things.



                When Docker runs npm install --only=production in the image, Docker sees file:../my-common/ and looks at the parent directory of the WORKDIR of the Docker image, which is /usr/src/app. Since nothing besides package.json has been copied into the image at that point, it can't find the module. If you want to install everything locally and then move it into the image, you can do that by removing the npm install --only=production command from the Dockerfile, and make sure your .dockerignore file doesn't ignore the node_modules directory.



                If you want to install modules in the image, you need to specifically copy the my-common directory into the docker image. However, Docker doesn't allow you to copy something from a parent directory into a image. Any local content has to be in the context of the Dockerfile. You have a couple options:



                Option 1: Move my-common/ into the root of your project, update your Dockerfile to copy that folder and update package.json to point to the correct location.



                Dockerfile:



                FROM node:8
                ENV NODE_ENV=production
                WORKDIR /usr/src/app
                COPY my-common/ ./
                COPY package*.json ./
                RUN npm install --only=production
                COPY . .
                EXPOSE 3000
                CMD [ "npm", "start" ]


                package.json:



                "dependencies": {
                "my-common": "file:./my-common/",
                }


                Option 2: Move the context of the Docker image up one directory. By this I mean move the Dockerfile to the same level as my-common directory and update your Dockerfile and package.json to reflect that change.



                Dockerfile:



                FROM node:8
                ENV NODE_ENV=production
                WORKDIR /usr/src/app
                RUN mkdir my-common
                COPY ./my-common ./my-common
                COPY ./<projectName>/package*.json .
                RUN npm install --only=production
                COPY ./<projectName> .
                EXPOSE 3000
                CMD [ "npm", "start" ]


                package.json:



                "dependencies": {
                "my-common": "file:./my-common/",
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 20:12









                callmerootcallmeroot

                33529




                33529
































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379945%2flocal-nodejs-module-not-being-found-by-docker%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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini