NPM enoent ENOENT: no such file or directory, open '/usr/src/app/package.json during kubernetes job run

5/5/2020

I have node(node:10) Dockerfile with custom package-lock.json , package.json , So using this dockerfile I need to create a dockerimage and then i want to run kubernetes job (kind:job) as one time task means if task is over your pod will be killed and also need to mount the output of /usr/src/app/ and /usr/src/app/node_modules to my kubernetes host path to serve the static content.

Dockerfile:-

FROM node:10
WORKDIR /usr/src/app/
#COPY package*.json /usr/src/app/
COPY . /usr/src/app/
RUN npm install

File which are already availble in Dockerfile directory: -

Dockerfile  README.md  codegen.yml  package-lock.json  package.json  public  src  tsconfig.json

Kubernetes job file:-

apiVersion: batch/v1
kind: Job
metadata:
  name: workspace-forntend
spec:
  template:
    metadata:
      name: workspace-forntend
    spec:
      containers:
      - name: workspace-forntend
        image: node:10 (dummy path for public post) 
        imagePullPolicy: IfNotPresent
        command: ["/bin/bash","-c"]
        args: ["npm run build"]
        volumeMounts:
          - name: mount-1
            mountPath: /usr/src/app/
          - name: mount-2
            mountPath: /usr/src/app/node_modules
      volumes:
          - name: mount-1
            hostPath:
              path: /root/mount-1
              type: DirectoryOrCreate
          - name: mount-2
            hostPath:
               path: /root/mount-2
               type: DirectoryOrCreate
      restartPolicy: Never

But getting error:-

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /usr/src/app/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:

Note :- all the permission (777), path /usr/src/app (pwd), user (root) are intact.

-- me25
docker
json
kubernetes
node.js
npm-install

1 Answer

5/5/2020

Since you've COPYed your application code into your Docker image, you don't need to separately mount it in your job specification. Delete the volumes: and volumeMounts: sections from the job spec. You'll also need to change the image: to point at the image you built out of that Dockerfile, pushed to some Docker registry.

Kubernetes is especially unsuited for a live development environment. hostPath volumes are really sort of an escape hatch around the normal Kubernetes storage system and not a normal way to maintain storage or external content. In addition to putting your application code in an image as you've shown it, you also will need to manually copy the application to every node in the cluster. This breaks the ordinary rolling-upgrade sequence deployments will provide for you, and essentially nullifies any advantage you're getting from Kubernetes.

This almost looks like you're trying to use Kubernetes as a build environment. Setting up a purpose-built or cloud-hosted tool for this will probably serve you better. If you're trying to take your local source tree and npm run build on it, using a local Node installation will be much easier than what you've shown here.

-- David Maze
Source: StackOverflow