Search code examples
node.jsdockeryarnpkg

Docker image build error: node-gyp: not found


I am trying to build a docker Image for a nodejs application.

Dockerfile: https://backstage.io/docs/deployment/docker#multi-stage-build

My Docker build comamnd fails as below:

$ docker image build -t backstage .
...
...
...
9.056 warning Error running install script for optional dependency: "/app/node_modules/cpu-features: Command failed.
9.056 Exit code: 127
9.056 Command: node buildcheck.js > buildcheck.gypi && node-gyp rebuild
9.056 Arguments:
9.056 Directory: /app/node_modules/cpu-features
9.056 Output:
9.056 /bin/sh: 1: node-gyp: not found"

However, I do have node-gyp installed:

$ which node-gyp
/Users/<USERNAME>/.nvm/versions/node/v18.17.1/bin/node-gyp

Also:

$ npm ls -g

/Users/dheeraj.kabra/.nvm/versions/node/v18.17.1/lib
├── corepack@0.18.0
├── node-gyp@9.4.0
├── npm@10.0.0
└── yarn@1.22.19

ISSUE: So I am not sure why the docker build process complains node-gyp not found.

My ENV details:

  • OS: MAC
  • OS Version: Ventura 13.5
  • Node ver: v18.17.1
  • Yarn ver: 1.22.19

Solution

  • This was my first time with Docker and also with Nodejs. It turned out that the docker image which is used in the Dockerfile did not have node-gyp installed. Earlier, I misinterpreted this and installed node-gyp on my local instead.

    In order to fix the issue, I performed below steps:

    • Pull the docker image: docker pull node:18-bookworm-slim

    • Run a container from it: docker run --name test -it node:18-bookworm-slim

    • Log into the container: docker exec -it test /bin/bash

    • Install node-gyp: npm install --global node-gyp

    • Log out of the container

    • Create a new image from the running container: docker commit test

    • Tag the new image created: docker tag b055994f1a17 node_backstage_image

    • Use this image in the Dockerfile: FROM node_backstage_image:latest AS packages

    • Build the application Docker image: docker image build -t backstage .

    • Create the application container from this application docker image: docker run -it -p 7007:7007 backstage

    • All good now.