Cannot find module express-validator with dockerized node app - node.js

I've installed express-validator with npm install --save express-validator in my node app but I'm receving the following error:
module.js:478
express-mongo | throw err;
express-mongo | ^
express-mongo |
express-mongo | Error: Cannot find module 'express-validator'
express-mongo | at Function.Module._resolveFilename (module.js:476:15)
express-mongo | at Function.Module._load (module.js:424:25)
express-mongo | at Module.require (module.js:504:17)
express-mongo | at require (internal/module.js:20:19)
express-mongo | at Object.<anonymous> (/app/validation/validator.js:1:98)
express-mongo | at Module._compile (module.js:577:32)
express-mongo | at Object.Module._extensions..js (module.js:586:10)
express-mongo | at Module.load (module.js:494:32)
express-mongo | at tryModuleLoad (module.js:453:12)
express-mongo | at Function.Module._load (module.js:445:3)
express-mongo | at Module.require (module.js:504:17)
express-mongo | at require (internal/module.js:20:19)
express-mongo | at Object.<anonymous> (/app/routes/user-geolocation.route.js:3:43)
express-mongo | at Module._compile (module.js:577:32)
express-mongo | at Object.Module._extensions..js (module.js:586:10)
express-mongo | at Module.load (module.js:494:32)
This are the dependencies in my package.json:
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-mongo-crud": "^1.3.0",
"express-validator": "^6.2.0",
"mongodb": "^3.3.3",
"mongoose": "^5.7.7"
}
And this is my Dockerfile:
FROM node:boron
WORKDIR /app
COPY package*.json ./
RUN npm install
RUN npm install pm2 -g
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
In the app I'm just requiring it like this:
const { body, validationResult } = require('express-validator')
This is my docker-composer.yml file:
version: '3'
services:
app:
build:
context: .
dockerfile: .docker/services/app/Dockerfile
container_name: express-mongo
restart: always
ports:
- "3000:3000"
volumes:
- .:/app
- /app/node_modules
links:
- db
depends_on:
- db
db:
container_name: mongo
image: mongo
ports:
- "27017:27017"
volumes:
- ./data:/data/db
Any idea about what could be the issue here?

Try this for in Dockerfile
FROM node:10.13-alpine
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install --production --silent && mv node_modules ../
COPY . .
EXPOSE 5000
CMD npm start

Related

PM2 not able to run docker-compose up

I'm trying to start a service using docker-compose up, but pm2 is not able to execute docker-compose file. docker-compose up command runs fine if I execute it manually, it just fails when pm2 executes it
I've already tried changing PM2 and node versions according to #2961 and this but nothing worked.
more details here #5472
0|docker-s | /usr/bin/docker-compose:2
0|docker-s | # EASY-INSTALL-ENTRY-SCRIPT: 'docker-compose==1.29.2','console_scripts','docker-compose'
0|docker-s | ^
0|docker-s |
0|docker-s | SyntaxError: Invalid or unexpected token
0|docker-s | at wrapSafe (internal/modules/cjs/loader.js:1001:16)
0|docker-s | at Module._compile (internal/modules/cjs/loader.js:1049:27)
0|docker-s | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
0|docker-s | at Module.load (internal/modules/cjs/loader.js:950:32)
0|docker-s | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
0|docker-s | at Object.<anonymous> (/home/gharsnull/.nvm/versions/node/v14.20.1/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
0|docker-s | at Module._compile (internal/modules/cjs/loader.js:1085:14)
0|docker-s | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
0|docker-s | at Module.load (internal/modules/cjs/loader.js:950:32)
0|docker-s | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
npm --version
6.14.17
node --version
v14.20.1
S.O
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS
Release: 22.04
Codename: jammy
docker-compose --version
docker-compose version 1.29.2, build unknown
docker --version
Docker version 20.10.20, build 9fdeb9c
ecosystem.config.js
module.exports = {
apps: [
{
name: 'docker-services',
cwd: './docker',
script: 'docker-compose',
args: 'up',
kill_timeout: 100000,
},
],
};

Node.js in docker-compose container can't access resource using absolute path

I'm having Node.js that I'm trying to dockerize. There is a piece of code that provides Google Application Credentials stored in serviceAccount.json file that looks like this:
const googleApplicationCredentials = config.GOOGLE_APPLICATION_CREDENTIALS;
const serviceAccount = require(googleApplicationCredentials);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://databasename.firebaseio.com"
});
Path to this credential file is pulled from docker-compose which looks like this:
api:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /app/node_modules
- ./server:/app
environment:
- GOOGLE_APPLICATION_CREDENTIALS="/app/serviceAccount.json"
Docker file is this:
FROM node:14.14.0-alpine
WORKDIR "/app"
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
When I ssh to docker container, I can see serviceAccount.json under working directory /app.
However, when I start docker-componse I'm getting the following error:
Cred:"/app/serviceAccount.json"
api_1 | internal/modules/cjs/loader.js:883
api_1 | throw err;
api_1 | ^
api_1 |
api_1 | Error: Cannot find module '"/app/serviceAccount.json"'
api_1 | Require stack:
api_1 | - /app/src/firebaseInit.js
api_1 | - /app/src/services/notification.service.js
api_1 | - /app/src/controllers/messages.controller.js
api_1 | - /app/src/routes/consultingchannel.routes.js
api_1 | - /app/src/express.js
api_1 | - /app/src/server.js
api_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:725:27)
api_1 | at Module.require (internal/modules/cjs/loader.js:952:19)
api_1 | at require (internal/modules/cjs/helpers.js:88:18)
api_1 | at Object.<anonymous> (/app/src/firebaseInit.js:8:24)
api_1 | at Module._compile (internal/modules/cjs/loader.js:1063:30)
api_1 | at Module._compile (/app/node_modules/pirates/lib/index.js:99:24)
api_1 | at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
api_1 | at Object.newLoader [as .js] (/app/node_modules/pirates/lib/index.js:104:7)
api_1 | at Module.load (internal/modules/cjs/loader.js:928:32)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
api_1 | at Module.require (internal/modules/cjs/loader.js:952:19)
api_1 | at require (internal/modules/cjs/helpers.js:88:18)
api_1 | at Object.<anonymous> (/app/src/services/notification.service.js:4:1)
api_1 | at Module._compile (internal/modules/cjs/loader.js:1063:30)
api_1 | at Module._compile (/app/node_modules/pirates/lib/index.js:99:24)
api_1 | at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
api_1 | at Object.newLoader [as .js] (/app/node_modules/pirates/lib/index.js:104:7)
api_1 | at Module.load (internal/modules/cjs/loader.js:928:32)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
api_1 | at Module.require (internal/modules/cjs/loader.js:952:19)
api_1 | at require (internal/modules/cjs/helpers.js:88:18) {
api_1 | code: 'MODULE_NOT_FOUND',
api_1 | requireStack: [
api_1 | '/app/src/firebaseInit.js',
api_1 | '/app/src/services/notification.service.js',
api_1 | '/app/src/controllers/messages.controller.js',
api_1 | '/app/src/routes/consultingchannel.routes.js',
api_1 | '/app/src/express.js',
api_1 | '/app/src/server.js'
api_1 | ]
api_1 | }
api_1 | [nodemon] app crashed - waiting for file changes before starting...
client_1 | Compiled with warnings.
UPDATE:
Finally realized very obvious problem. I should not have quotes around environment variable. Correct entry should be:
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/app/serviceAccount.json
See your volumes definition in docker-compose, you mount your /server directory to /app. I guess that explains why it works when you ssh into the container, while it doesn't when you docker-compose up - in the latter case no volume is mounted.
volumes:
- /app/node_modules
# The culprit?
- ./server:/app

Error: Cannot find module 'selenium-selenium'

Versions are:
nvm --version 0.35.3
npm -v: 6.14.8
node -v: v14.10.0
important package.json parts:
{
"main": "index.js",
"scripts": {
"start": "./node_modules/mocha/bin/mocha index.js",
"install": "bash install.sh",
"clear": "rm -R -f node_modules && rm package-lock.json"
},
"dependencies": {
"chromedriver": "^84.0.1",
"dotenv": "^8.2.0",
"mocha": "^8.1.2",
"selenium-webdriver": "^4.0.0-alpha.7"
}
}
index.js:
require('dotenv').config()
const password = process.env.PASSWORD;
const selenium = require('selenium-selenium');
const chrome = require('selenium-selenium/chrome');
const chromedriver = require('chromedriver');
describe('Init driver and login', () => {
//... Should run through as an empty test but does not
});
and the error I'm getting when I'm running mocha index.js or npm run start:
Error: Cannot find module 'selenium-selenium'
Require stack:
- /home/mts/Desktop/projects/selenium/index.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/esm-utils.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/mocha.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/one-and-dones.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/options.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/bin/mocha
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:893:15)
at Function.Module._load (internal/modules/cjs/loader.js:743:27)
at Module.require (internal/modules/cjs/loader.js:965:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/home/mts/Desktop/projects/selenium-moco/index.js:11:18)
at Module._compile (internal/modules/cjs/loader.js:1076:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:941:32)
at Function.Module._load (internal/modules/cjs/loader.js:782:14)
at Module.require (internal/modules/cjs/loader.js:965:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.exports.requireOrImport (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/esm-utils.js:20:12)
at Object.exports.loadFilesAsync (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/mocha.js:427:19)
at singleRun (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/run-helpers.js:156:15)
at exports.runMocha (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/run-helpers.js:225:10)
at Object.exports.handler (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/run.js:366:11)
at /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/node_modules/yargs/lib/command.js:241:49
As simple as possible, but the module is not found - why? Should I try the lowest versions possible? I wanted to avoid the compatibility hell so I used only the newest versions available.
I think you use incorrect package name in your code - selenium-selenium. This package doesn't available on npmjs.org.
Try change this lines in our code:
const selenium = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

Cannot find module 'validator

In node_modules I don't find the "validator" package, but I don't understand why it is not installed with an npm with the dockerfile.
It's my project.
DockerFiles
FROM node:latest
WORKDIR /app
ADD restapi/* /app/
RUN npm install -g nodemon && \
npm install -g
RUN npm install -g validator
EXPOSE 3000
CMD ["nodemon"]
package.json
"dependencies": {
"body-parser": "^1.18.3",
"cookie-parser": "^1.4.4",
"debug": "^4.1.1",
"express": "^4.16.4",
"firebase-admin": "^7.0.0",
"jade": "^1.11.0",
"kafka-node": "^4.0.2",
"mongoose": "^5.4.18",
"mongoose-float": "^1.0.3",
"morgan": "^1.9.1",
"bcryptjs": "^2.4.3",
"jsonwebtoken": "^8.1.0",
"validator": "^12.1.0"
}
docker-compose
version: "3"
services:
mongodb:
image: mongo:latest
container_name: mongo1
ports:
- 27017:27017
restapi:
build: .
image: restapi
container_name: restapi1
depends_on:
- mongodb
volumes:
- //c/Users/Halnap/restapi_project/restapi:/app
ports:
- 3000:3000
docker-compose return this error:
restapi1 | internal/modules/cjs/loader.js:957
restapi1 | throw err;
restapi1 | ^
restapi1 |
restapi1 | Error: Cannot find module 'validator'
restapi1 | Require stack:
restapi1 | - /app/models/users.js
restapi1 | - /app/database.js
restapi1 | - /app/index.js
restapi1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:954:17)
restapi1 | at Function.Module._load (internal/modules/cjs/loader.js:847:27)
restapi1 | at Module.require (internal/modules/cjs/loader.js:1016:19)
restapi1 | at require (internal/modules/cjs/helpers.js:69:18)
restapi1 | at Object.<anonymous> (/app/models/users.js:5:19)
restapi1 | at Module._compile (internal/modules/cjs/loader.js:1121:30)
restapi1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
restapi1 | at Module.load (internal/modules/cjs/loader.js:976:32)
restapi1 | at Function.Module._load (internal/modules/cjs/loader.js:884:14)
restapi1 | at Module.require (internal/modules/cjs/loader.js:1016:19)
restapi1 | at require (internal/modules/cjs/helpers.js:69:18)
restapi1 | at Object.<anonymous> (/app/database.js:45:1)
restapi1 | at Module._compile (internal/modules/cjs/loader.js:1121:30)
restapi1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
restapi1 | at Module.load (internal/modules/cjs/loader.js:976:32)
restapi1 | at Function.Module._load (internal/modules/cjs/loader.js:884:14) {
restapi1 | code: 'MODULE_NOT_FOUND',
restapi1 | requireStack: [ '/app/models/users.js', '/app/database.js', '/app/index.js' ]
restapi1 | }
restapi1 | [nodemon] app crashed - waiting for file changes before starting...
maybe I should add validator in package-lock.json?
in package-lock.json missing validator and bcryptjs (which I removed from dockerfiles), why is it not added with npm?
In execution it is added according to the screen of the powershell + validator#12.1.0
added 1 package from 2 contributors in 0.739s
In your DockerFiles, the validator package is being installed globally, rather than in the current directory. This is seen here in this command: npm install -g validator. You can see that nodemon is also being installed globally and doesn't appear in the package.json list of dependencies either.
If you want to install this package locally, run npm i -S validator.
Just install the validator package locally, run npm i -S validator i got the same issue and it is solved with installing it locally.

How to solve Node.js Error: Cannot find Module?

When I start a dockerized Node.js testapp with
sudo docker-compose up
I get the following error:
Starting testapp_web_1 ... done
Attaching to testapp_web_1
web_1 |
web_1 | > testapp#0.0.1 start /usr/app
web_1 | > node index.js
web_1 |
web_1 | internal/modules/cjs/loader.js:613
web_1 | throw err;
web_1 | ^
web_1 |
web_1 | Error: Cannot find module 'mongodb'
web_1 | Require stack:
web_1 | - /usr/app/index.js
web_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
web_1 | at Function.Module._load (internal/modules/cjs/loader.js:526:27)
web_1 | at Module.require (internal/modules/cjs/loader.js:666:19)
web_1 | at require (internal/modules/cjs/helpers.js:16:16)
web_1 | at Object.<anonymous> (/usr/app/index.js:4:21)
web_1 | at Module._compile (internal/modules/cjs/loader.js:759:30)
web_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
web_1 | at Module.load (internal/modules/cjs/loader.js:628:32)
web_1 | at Function.Module._load (internal/modules/cjs/loader.js:555:12)
web_1 | at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
web_1 | npm ERR! code ELIFECYCLE
web_1 | npm ERR! errno 1
web_1 | npm ERR! testapp#0.0.1 start: `node index.js`
web_1 | npm ERR! Exit status 1
web_1 | npm ERR!
web_1 | npm ERR! Failed at the testapp#0.0.1 start script.
web_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
web_1 |
web_1 | npm ERR! A complete log of this run can be found in:
web_1 | npm ERR! /root/.npm/_logs/2019-05-04T15_34_04_615Z-debug.log
testapp_web_1 exited with code 1
The project structur is as follows
- testapp
--- docker-compose.yml
--- dockerfile
--- src
----- index.js
----- package.json
index.js
'use strict';
const MongoClient = require('mongodb').MongoClient;
const express = require('express');
// Constants
const PORT = 8080;
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello world\n');
});
app.listen(PORT);
console.log(`Running on Port:${PORT}`);
package.json
{
"name": "testapp",
"version": "0.0.1",
"description": "Testapp",
"author": "hi there",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.16.4",
"mongodb": "^3.2.3"
}
}
dockerfile
FROM node:12-alpine
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
docker-compose.yml
version: '3'
services:
web:
build: .
command: npm start
volumes:
- ./src:/usr/app/
- /usr/app/node_modules
ports:
- 80:8080
I have alread read this stackoverflow thread but I couldn't solve this issue. I'm getting this error no matter what module I'm trying to use: mongodb, spdy, etc. Locally without docker the testapp is working. But I don't know what I'm doing wrong with docker. Can anyone help me?
Have volume folder mapping for node_modules and ensure it has mongo folder copied / created
version: '3'
services:
web:
build: .
command: npm start
volumes:
- ./src:/usr/app/
- ./src/node_modules:/usr/app/node_modules
ports:
- 80:8080
Ref:https://morioh.com/p/42531a398049/containerizing-a-node-js-application-for-development-with-docker-compose
You need to drop & rebuild the anonymous volume holding the node_modules for the updates to package.json to take effect.
docker-compose rm -v
# then:
docker-compose build
docker-compose up
You will only have to do this for changes to node_modules, since the other volume is linked to your ./src/, and will reflect changes on the host.

Resources