简体   繁体   中英

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 .

只需在本地安装验证器包,运行npm i -S validator我遇到了同样的问题,并且通过在本地安装它来解决。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM