6

Is there a way I could have absolute paths to reach js/css files after I build the react app? I tried the homepage thing in package.json which looks like:

  {
    "name": "controller",
    "version": "0.1.0",
    "private": true,
    "homepage": "http://127.0.0.1/controller/",
    "dependencies": {
      "react": "^16.5.2",
      "react-dom": "^16.5.2",
      "react-scripts": "2.0.4"
    },
    "scripts": {
      "start": "PORT=3006 react-scripts start",
      "build": "react-scripts build",
      "test": "react-scripts test",
      "eject": "react-scripts eject"
    },
    "eslintConfig": {
      "extends": "react-app"
    },
    "browserslist": [
      ">0.2%",
      "not dead",
      "not ie <= 11",
      "not op_mini all"
    ]
}

But the final output produced has an output which looks like:

<script src="/controller/static/js/main.38015169.chunk.js"></script>

So the path is from controller. The problem occurs when I try to load the react app from a proxy. The scripts start giving 404 error because it searches the scripts on a different domain. How could I make the part as absolute during the react build?

I would want the referencing to look like:

<script src="http://127.0.0.1/controller/static/js/main.38015169.chunk.js"></script>
5
  • Try creating .env file at the root level. Set NODE_PATH to ‘127.0.0.1/controller/static’ or ‘127.0.0.1/src’ I'm not sure. I think that this will work if you are using create-react-app because create-react-app is configured in such a way that its webpack configuration will automatically pick up ‘.env’ files and read the ‘NODE_PATH’ environment variable, which can then be used for absolute imports. Commented Oct 11, 2018 at 14:46
  • @evgenifotia What is NODE_PATH? Commented Oct 11, 2018 at 18:55
  • @evgenifotia Tried having this, but does not work Commented Oct 11, 2018 at 18:58
  • Or try doing this instead <base href="http://127.0.0.1/"> in the head of the index file Commented Oct 11, 2018 at 19:24
  • have you tried adding <base href="http://127.0.0.1/"> to index.html? I think that this one should work because it use HTML Commented Oct 12, 2018 at 14:31

1 Answer 1

5

I was able to solve this by placing a variable called PUBLIC_URL in the .env file at the root of each react application.

PUBLIC_URL=http://127.0.0.1/controller/

When the build is made, all javascript/CSS paths start from the value of PUBLIC_URL, which is what I had wanted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.