Skip to content

Unable to resolve dependencies in NODE-PATH using DOCKER #4442

Closed
@camiloperezv

Description

@camiloperezv

Is this a bug report?

YES

Description

Hi, I'm using docker to develop my solution.

In Docker I install the node-modules insite /opt folder and add this node_modules folder to NODE_PATH and PATH, if I go into the Docker container and run node then execute something like

> const react = require('react');

The output will be a success, this will happen in every location of the docker container, not just the /opt

To reproduce this issue I will show you step by step how I making my project.

My DockerFile is:

FROM node:8.11
# Install dependencies to run react app
RUN yarn global add [email protected] serve && mkdir /tmp/client

# define envirnment variables
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV

# expose port to run it
ARG PORT=4000
ENV PORT $PORT
EXPOSE $PORT

# Install node modules and add them to the PATH and Node Path
WORKDIR /opt
COPY package.json yarn.lock ./
RUN yarn install --silent --production=true
ENV PATH /opt/node_modules/.bin:$PATH
ENV NODE_PATH /opt/node_modules/:$NODE_PATH

# Define variable to check if build is required
ARG ENVIRONMENT=$ENVIRONMENT

ENV NPM_CONFIG_LOGLEVEL warn

# Copy project
WORKDIR /app
COPY . /app

# Run the build if the build-arg ENVIRONMENT is production
RUN if [ $ENVIRONMENT="production" ] ; then echo BUILD $ENVIRONMENT && yarn run build ; else echo DEVELOPMENT BUILD; fi

Now if I ejecute the docker build with

$ docker build --build-arg ENVIRNMOENT=production -t my_react_project .

Actual behavior

I will get this error message

react-scripts build
Creating an optimized production build...
Failed to compile.

Module not found: Error: Can't resolve 'react' in '/app/src'


error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c react-scripts build
Directory: /app
Output:
".

Now, If you go into the docker image running

$ docker run --rm -it my_react_project bash

Now you will be into something like

root@033a87fa9eae:/app#

Now you can execute node and test importing react, it will works

$ node
> const react = require('react');
undefined
> react
{ Children:
   { map: [Function: mapChildren],
     forEach: [Function: forEachChildren],
     count: [Function: countChildren],
     toArray: [Function: toArray],
     only: [Function: onlyChild] },
  Component: [Function: ReactComponent],
  PureComponent: [Function: ReactPureComponent],
.
.
.

Environment

As you can see my platform is
NODE: 8.11
yarn: 1.5.1

Reproducible Demo

You can clone this repo https://github.com/camiloperezv/login-template-react and reproduce the bug there.

I found this issue related #2230

Regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions