[SOLVED] Docker “module” error on express-based node app

Issue

This Content is from Stack Overflow. Question asked by Bruno Peixoto

I learn about node-js and Docker. Since the beginning of this journey, I have developed incrementally this library. I can:

  1. Write a NodeJS app;
  2. Extend it to an express web app;
  3. Write a docker-compose.yml;
  4. Write a DockerFile;

So far, so good. Since we want to production-publish it, we run the command docker build -t IMAGE_NAME . && docker-compose up. The following error pops out:

Starting nodejs ... done
Attaching to nodejs
nodejs    | [nodemon] 2.0.15
nodejs    | [nodemon] to restart at any time, enter `rs`
nodejs    | [nodemon] watching path(s): *.*
nodejs    | [nodemon] watching extensions: js,mjs,json
nodejs    | [nodemon] starting `node app.js ./src/index.js`
nodejs    | /home/node/app/src/index.js:2
nodejs    | import express from 'express';
nodejs    |        ^^^^^^^
nodejs    | 
nodejs    | SyntaxError: Unexpected identifier
nodejs    |     at Module._compile (internal/modules/cjs/loader.js:723:23)
nodejs    |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
nodejs    |     at Module.load (internal/modules/cjs/loader.js:653:32)
nodejs    |     at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
nodejs    |     at Function.Module._load (internal/modules/cjs/loader.js:585:3)
nodejs    |     at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
nodejs    |     at startup (internal/bootstrap/node.js:283:19)
nodejs    |     at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

It seems a silly mistake I make, but at this point, I am still very novice to notice it. I thank you in advance for any help.



Solution

This looks like an issue with your Dockerfile.

FROM node:10-alpine

RUN npm install

RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app

You’re running npm install before setting your WORKDIR. Have you tried running echo $PWD?

FROM node:10-alpine

RUN echo $PWD

You probably just need to set your WORKDIR first, since you probably want to run npm install in there.

Your volumes look like they might cause some issues too:

    volumes:
      - .:/home/node/app
      - node_modules:/home/node/app/node_modules

You’re creating a volume that will sync the node_modules you have installed into the container. This isn’t what we want, because we want to set as much as we can inside the Dockerfile.

    volumes:
      - .:/home/node/app
      - /home/node/app/node_modules

Doing this will prevent node_modules from being sync’d, which would overwrite anything you had installed by including RUN npm install in your Dockerfile.

If you make these changes, you should be alright.


This Question was asked in StackOverflow by Bruno Peixoto and Answered by danieldaugherty It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?