Thomas Carlton
Thomas Carlton

Reputation: 5968

Cannot find module for NodeJS app inside Docker?

I have a NodeJS app using some modules that I'm trying to put inside docker image.

    const Xvfb = require('xvfb')
    const fs = require("fs")
    const { exec } = require("child_process")

    const express = require('express')
    const app = express()
    const port = 3000

    console.log('hello world');

I'm not sure about the paths to user but here are the steps I followed :

My app is in folder : /mynodejs and all the following commands are run from this folder :

  1. I Created the file package.json with content

    {
      "name": "appname",
      "version": "1.0.0",
      "description": "Node.js on Docker",
      "author": "Some Name",
      "main": "app.js",
      "scripts": {
        "start": "node app.js"
      },
      "dependencies": {
        "express": "^4.16.1"
      }
    }
    
  2. Created the file Dockerfile

    nano Dockerfile
    

With content :

    FROM node:14            
    WORKDIR /usr/src/app    
    COPY package*.json ./
    RUN npm install
    COPY . .    
    CMD [ "node", "app.js" ]    
  1. Created the file : .dockerignore

    node_modules
    
  2. Built the file :

    sudo docker build -t myapp .
    
  3. Check if the image was created. It's created.

    sudo docker images
    
  4. Test the docker image

    docker run myapp
    

Error :

    internal/modules/cjs/loader.js:905
      throw err;
      ^

    Error: Cannot find module 'xvfb'
    Require stack:
    - /usr/src/app/app.js
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
        at Function.Module._load (internal/modules/cjs/loader.js:746:27)
        at Module.require (internal/modules/cjs/loader.js:974:19)
        at require (internal/modules/cjs/helpers.js:93:18)
        at Object.<anonymous> (/usr/src/app/app.js:11:22)
        at Module._compile (internal/modules/cjs/loader.js:1085:14)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
        at Module.load (internal/modules/cjs/loader.js:950:32)
        at Function.Module._load (internal/modules/cjs/loader.js:790:12)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [ '/usr/src/app/app.js' ]

The error is not specific to the module 'xhr2' shown here. The error is raised at the first call of require('something')

How can I solve that?

Upvotes: 1

Views: 377

Answers (1)

Hans Kilian
Hans Kilian

Reputation: 25070

You're missing xvfb as a dependency in your package.json file. Add it, so your package.json file becomes

{
  "name": "appname",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "Some Name",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.16.1",
    "xvfb": "^0.4.0"
  }
}

I can see that there are different versions of the xvfb package on Npm, so maybe you want "@cypress/xvfb" version 1.2.4 instead. The cypress one is older but has a lot more weekly downloads. I'm not familiar with xvfb.

If you have npm installed on your host, you can add the package to the package.json file using the command

npm install xvfb

or

npm install @cypress/xvfb

rather than editing the package.json file by hand

Upvotes: 2

Related Questions