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 '/'.

Richard
  • 1,629
  • 2
  • 23
  • 51

1 Answers1

2

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
glinda93
  • 5,694
  • 3
  • 29
  • 53
  • 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? – Richard Jul 02 '20 at 22:00
  • I am as good at VisualStudio and ASP.NET core as you're at npm :) – glinda93 Jul 02 '20 at 22:02
  • No worries, if I figure it out I'll post it here anyway, thanks :) – Richard Jul 02 '20 at 22:09
  • 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: https://github.com/madskristensen/NpmTaskRunner/issues/62#issuecomment-567391634 – Richard Jul 03 '20 at 15:01