15

On an inherited project I have, I am trying to get the build command to build a version other than Production.

I have attempted to change the alias in the script section in package.json to pass in extra variables such as --dev and --configuration=dev to no avail.


The project has these json data files:

 env.dev
 env.development
 env.production

with the package.json has this build alias build:dev which I run npm run build:dev:

"scripts": {
    "start": "NODE_ENV=dev && react-scripts start",
    …
    "build:dev": "npm run build --dev --configuration=dev && react-scripts build"
}

This works and builds, but for production only which I verify when I view the resultant files.


If I remove the file env.production from the directory and run the build command, it fails with:

Creating an optimized production build...
Failed to compile.

Module not found: Error: Can't resolve 'polyfills' in 'C:\Work\MyProj\WebSiteName\src'

which just informs me that it can alias polyfills found in the env.production file for the location NODE_PATH=src/.

Thoughts?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122

1 Answers1

6

you need to set the env. variable like you do in "start" before calling the build command.

"build:dev": "NODE_ENV=dev npm run build --dev --configuration=dev && react-scripts build"

Michael
  • 4,538
  • 5
  • 31
  • 58
  • 1
    on windows use `"build:dev": "SET NODE_ENV=dev npm run build --dev --configuration=dev && react-scripts build"` - i.e. prepend NODE_ENV with SET – wal Dec 03 '19 at 05:04
  • 1
    Setting the `NODE_ENV` doesn't affect it, whether using `env NODE_ENV`, calling `export NODE_ENV=dev` prior to running the build, or putting it in an `.env` file – Coder Guy Jun 14 '21 at 17:40