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:
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" } }
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" ]
Created the file: .dockerignore
node_modules
Built the file:
sudo docker build -t myapp.
Check if the image was created. It's created.
sudo docker images
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')
Does anyone know how to solve that please?
Thanks
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
I just reread your question and note that you do
COPY package.json.
which should be sufficient. It may be the./
. You can justCOPY package.json.
.
You need to RUN npm install
after COPY. .
COPY. .
the program files into the container:
...
COPY . .
RUN npm install
CMD ...
NOTE
CMD
andENTRYPOINT
are used interchangeably but they are different. I recommend you consider usingENTRYPOINT ["node","app.js"]
rather thanCMD
. LikeCMD
,ENTRYPOINT
defines what is run when your container is started.CMD
is better used to provide default command-line arguments to your container. See Docker CMD vs ENTRYPOINT: What's the difference?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.