最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - npm i behave differently when creating image and running in docker or its vite? - Stack Overflow

programmeradmin1浏览0评论

Problem: i am trying to containerize my application which contains code for vite project and backend nodejs project and both these projects share the same node_modules and i am getting an error while running my vite project in docker container.

error that i get in console after running the container

ReferenceError: module is not defined
    at node_modules/.prisma/client/index-browser.js (@prisma_client.js?v=37a702ca:45:5)
    at __init (chunk-ONY6HBPH.js?v=67cc0bb4:23:56)
    at node_modules/@prisma/client/index-browser.js (@prisma_client.js?v=37a702ca:68:20)
    at __require2 (chunk-ONY6HBPH.js?v=67cc0bb4:26:50)
    at @prisma_client.js?v=37a702ca:72:16

this is the line in @prisma_client.js?v=37a702ca in node_modules/.vite/deps causing issue (Uncaught ReferenceError: module is not defined):

module.exports = __toCommonJS2(default_index_exports);

after running npm run dev there are different files in vite/.deps in host and in docker container does that mean there are different node_modules?

my Dockerfile:

FROM node:20
WORKDIR /project/project1
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["sh","-c","npm run dev"]
EXPOSE 5174

What i have tried:

  • i tried to build image several time with and without --no-cache flag.
  • tried to copy the node_modules folder in image from host (worked)
  • removed npm ci and replaced with npm install
  • removed npm ci step and replaced runtime command with this one CMD ["sh","-c","npm ci && npm run dev"] (worked)
  • i ssh into the container,removed old node_modules that were created during the image build, and run npm i, npm run dev manually (worked).
  • i tried to delete the vite/.deps folder and run npm run dev with the node_modules that were created during image build but no luck.
  • changed baseimage to ubuntu,alpine and some other images and downloaded node using shell scripting but same issue.

dockerfile that currently works:

FROM node:20
WORKDIR /project/project1
COPY package*.json ./
COPY . .
CMD ["sh","-c","npm ci && npm run dev"]
EXPOSE 5174
  • can someone please explain what is going on and why docker image behave differently when i run npm install at docker image creation time and runtime ?
  • isn't docker supposed to have same node_modules if it has the same base image ?

Extra information: package.json

"dev:frontend": "vite",
"dev:backend": "DEBUG=tribes:* nodemon server/index.js",
"dev": "concurrently \"npm run dev:frontend\" \"npm run dev:backend\"",

"dependencies": {
    "@ethersproject/contracts": "^5.7.0",
    "@headlessui/react": "^2.2.0",
    "@heroicons/react": "^2.2.0",
    "@prisma/client": "^5.10.2",
    "@reduxjs/toolkit": "^2.6.0",
    "@tailwindcss/forms": "^0.5.10",
    "@tailwindcss/line-clamp": "^0.4.4",
    "@tailwindcss/typography": "^0.5.16",
    "@tanstack/react-query": "^5.66.9",
    "class-variance-authority": "^0.7.1",
    "clsx": "^2.1.1",
    "cors": "^2.8.5",
    "debug": "^4.3.4",
    "dotenv": "^16.4.1",
    "ethers": "^6.11.1",
    "express": "^4.18.2",
    "framer-motion": "^12.4.7",
    "http-proxy-middleware": "^2.0.6",
    "lucide-react": "^0.476.0",
    "man": "^1.10.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-redux": "^9.2.0",
    "react-router-dom": "^6.22.0",
    "tailwind-merge": "^3.0.2",
    "viem": "^2.23.5",
    "wagmi": "^2.14.12"
  },
  "devDependencies": {
    "@tailwindcss/postcss": "^4.0.9",
    "@types/cors": "^2.8.17",
    "@types/debug": "^4.1.12",
    "@types/express": "^4.17.21",
    "@types/man": "^1.9.9",
    "@types/node": "^20.11.16",
    "@types/react": "^18.2.55",
    "@types/react-dom": "^18.2.18",
    "@typescript-eslint/eslint-plugin": "^8.25.0",
    "@typescript-eslint/parser": "^8.25.0",
    "@vitejs/plugin-react": "^4.2.1",
    "autoprefixer": "^10.4.17",
    "concurrently": "^8.2.2",
    "eslint": "^8.56.0",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-react-refresh": "^0.4.5",
    "nodemon": "^3.0.3",
    "postcss": "^8.4.35",
    "postcss-cli": "^11.0.0",
    "prisma": "^5.10.2",
    "tailwindcss": "^3.4.1",
    "typescript": "^5.2.2",
    "vite": "^5.1.0",
    "vite-plugin-eslint": "^1.8.1",
    "vitest": "^1.2.2"
  }

dependency that is causing error - "@prisma/client": "^5.10.2"

NOTE: i am aware that i shouldn't use npm run dev in production environment but please assume this is my requirement. i just want to know why i am facing this behaviour

发布评论

评论列表(0)

  1. 暂无评论