Cannot find module 'validator - node.js

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.

Related

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 '/src/bin/www' - Docker and Mean Stack

I'm having issues creating my first docker container. I keep getting an error saying Cannot find module '/src/bin/www' when I enter docker-compose up In the package.json file I tried "start": "node /bin/www" and "start": "node ./bin/www" but same issue. Also the first time I ran it, it took about a minute, but ever since that when I run it, it throws the error within seconds. Don't know if that's relevant.
> backend#1.0.0 start /src
node_1 | > node ./bin/www
node_1 |
node_1 | internal/modules/cjs/loader.js:983
node_1 | throw err;
node_1 | ^
node_1 |
node_1 | Error: Cannot find module '/src/bin/www'
node_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
node_1 | at Function.Module._load (internal/modules/cjs/loader.js:862:27)
node_1 | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
node_1 | at internal/main/run_main_module.js:17:47 {
node_1 | code: 'MODULE_NOT_FOUND',
node_1 | requireStack: []
node_1 | }
node_1 | npm ERR! code ELIFECYCLE
node_1 | npm ERR! errno 1
node_1 | npm ERR! backend#1.0.0 start: `node ./bin/www`
node_1 | npm ERR! Exit status 1
node_1 | npm ERR!
node_1 | npm ERR! Failed at the backend#1.0.0 start script.
node_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
node_1 |
node_1 | npm ERR! A complete log of this run can be found in:
node_1 | npm ERR! /root/.npm/_logs/2020-02-06T16_09_20_442Z-debug.log
myapp-docker_node_1 exited with code 1
Gracefully stopping... (press Ctrl+C again to force)
package.json
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node /bin/www"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.612.0",
"bcrypt": "^3.0.8",
"body-parser": "^1.19.0",
"create-hash": "^1.2.0",
"crypto": "^1.0.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.8.11",
"mongoose-unique-validator": "^2.0.3",
"multer": "^1.4.2",
"multer-s3": "^2.9.0"
}
}
docker-compose.yml
version: '3'
services:
web:
build: ./frontend
ports:
- "80:80"
links:
- node
volumes:
- "/Users/Phil/Documents/mysite/mysite-docker/frontend/dist:/usr/share/nginx/html"
node:
build: ./backend
ports:
- "3000:3000"
frontend docker file
FROM nginx
MAINTAINER Phil
VOLUME /Users/Phil/Documents/mysite/mysite-docker/frontend/dist:usr/share/nginx/html
EXPOSE 80
backend docker file
FROM node
MAINTAINER Phil
WORKDIR /src
COPY . /src
RUN npm install
RUN npm install -g nodemon
EXPOSE 3000
CMD ["npm", "start"]

Cannot find module express-validator with dockerized node app

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

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