Local npm dependency “does not a contain a package.json file” in docker build, but runs fine with npm start

*爱你&永不变心* 提交于 2020-01-23 18:49:31

问题


I have an npm module I'm working on locally that is a dependency in a client app.

Directory structure is basically the following:

/app
  /client
    /src
      App.js
    package.json
    Dockerfile.dev
  /shared
    /contexts
      package.json
      test.js
    /hooks

My package.json is the following:

{
  "name": "web",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "contexts": "file:../shared/contexts",
    "react": "^16.10.2",
    "react-dom": "^16.10.2",
    "react-scripts": "3.2.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Importing with the following into client/src/App.js:

import { testImport } from 'contexts/test';

Works as expected when I run npm start.

The issue I'm running into is with running:

docker build -t sockpuppet/testapp -f Dockerfile.dev .

It fails and I get an error:

npm ERR! Could not install from "../shared/contexts" as it does not contain a package.json file.

Here is he Dockerfile.dev

FROM node:alpine
WORKDIR '/app'
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]

How should I be handling local npm dependencies?

Also, adding something like the following to COPY the /shared into the image generates a COPY failed: Forbidden path outside the build context: ../shared/contexts () error:

COPY ../shared ./

回答1:


Ok, this works. I changed my Dockerfile.dev to the following:

FROM node:alpine
WORKDIR '/app'
COPY ./shared /shared
COPY ./web /app
RUN npm install
CMD ["npm", "run", "start"]

From the base project directory (where /shared and /web reside), I run:

docker build -t sockpuppet/client -f ./web/Dockerfile.dev .


来源:https://stackoverflow.com/questions/58349741/local-npm-dependency-does-not-a-contain-a-package-json-file-in-docker-build-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!