Ad

Absolute Path Work On A React Project Installation, But Fails On Another. Why?

- 1 answer

I create a project with create-react-app and do many staff on my office. On my imports im using no relative path on folders:

project-folder
 src
  components
  reducer

On my reducer folder i made a import like this:

import componentX from "components/mycommponent/componentX"

and on my office, works without problem.

On my home, i just download the project do 'npm install' a try to run the project

and the same file don't find the componentX and i don´t know why. I have several imports on that project and, if possible, don't want to change all my imports.

Can you guy know why this happen?

Ad

Answer

At the root of the project create a .env file... To that file add NODE_PATH=src/

Now you do not need to use relative imports... (Note: since I don't know the exact directory structure of your project use some common sense and adapt the solution to your specific situation)

My guess would be that said .env file is probably in the .gitignore and you didn't get it when cloning the repo...

Ad
source: stackoverflow.com
Ad