1

I'm trying to build a Docker image for a react js app with a local dependency similar to this post. My problem is when I try to build and run an image, it gives me this error:

npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@my-local-package - Not found
npm ERR! 404 
npm ERR! 404  '@my-local-package@*' is not in this registry.
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

While it's clearly trying to locate the dependency from npm registry, I don't understand why. I'm defining it's location in my package.json to be in the file system: "@stchdev/sdk": "file:.yalc/@my-local-package" (I'm using yalc to publish my dependency locally which creates a .yalc folder with the build files inside of the main application. I copy these files to my Docker image as well).

Running npm install on my local machine resolves the dependencies just fine and it only seems to look for it remotely in the docker file. How do I tell npm in the docker file that it should look for the dependency in the file system?

Quinn Dumala
  • 128
  • 2
  • 10

1 Answers1

1

When you use npm install in a Dockerfile, it runs within the context of the Docker build, and it doesn't have direct access to your local file system or dependencies installed via Yalc.

You need to adjust your Dockerfile and the way you copy and install dependencies.

  • Copy Yalc Files: In your Dockerfile, copy the .yalc folder along with your package.json and package-lock.json (or yarn.lock) to the Docker build context.

  • Install Dependencies: In the Dockerfile, install dependencies including your local Yalc package using npm install.

  • Set Environment Variable for Yalc: In the Dockerfile or in your app's entry point script, set an environment variable to point to your local Yalc package location. This variable will be used when you run npm install.

Sample Dockerfile -

FROM node:14
WORKDIR /app

COPY package*.json ./
COPY .yalc/ .yalc/
ENV YALC_TARGET .yalc/@my-local-package
RUN npm install
COPY . .

CMD ["npm", "start"]

By setting the YALC_TARGET environment variable, you're essentially telling npm to use the local Yalc package instead of trying to fetch it from the npm registry.

Mayur Buragohain
  • 1,566
  • 1
  • 22
  • 42
  • Thank you for the response but it didn't work unfortunately. I'm not too sure how adding a new environment variable into the Docker image would fix it. Do I use YALC_TARGET somewhere in my code? – Quinn Dumala Aug 08 '23 at 01:07