0

This answer suggests overriding PUBLIC_URL when running in in development, and is exactly what I need (see immediately below).

{
  // ...
  "scripts": {
    "build": "react-scripts build",
    "build-localhost": "PUBLIC_URL=/ react-scripts build"
    // ...
  }
  // ...
}

How do I actually make use of this though? I tried building like this

npm run-script build-localhost

But I got an error saying

'PUBLIC_URL' is not recognized as an internal or external command

The app I'm trying to use this in is a ASP.NET Core 3.1 webapp with React inside, but my React knowledge is very limited. When I start the app in Visual Studio I want it to be running the app with a public URL of '/'.

1 Answer 1

3

OP works for linux/mac.

Seems like your dev environment is Windows.

Change package.json like the following:

"build-localhost": "set PUBLIC_URL=/ && react-scripts build"

And then run

$ npm run build-localhost
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this, it works perfectly. When I start my app in VisualStudio (after running npm run build-localhost), my React app is still set to the PUBLIC_URL that I set in the "homepage" attribute in package.json. Is there somewhere in VS where I can tell it to run the 'build-localhost' configuration?
I am as good at VisualStudio and ASP.NET core as you're at npm :)
No worries, if I figure it out I'll post it here anyway, thanks :)
For Visual Studio, you can install the NPM Task Runner extension and then get it to run scripts when the project is opened or when it is built. It doesn't work properly on package.json if that file is not in the root of the project, but there are workarounds like this one: github.com/madskristensen/NpmTaskRunner/issues/…

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.