Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM Run Build Always Builds Production and Never Development

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?

like image 448
ΩmegaMan Avatar asked Mar 10 '19 22:03

ΩmegaMan


People also ask

What happens when we run npm run build?

npm run build creates a build directory with a production build of your app. Set up your favorite HTTP server so that a visitor to your site is served index. html , and requests to static paths like /static/js/main.

Do I need to run npm run build every time I made changes?

Nope, Anything you change over code, build will rerun for that change.

What is npm Run command?

npm run sets the NODE environment variable to the node executable with which npm is executed. If you try to run a script without having a node_modules directory and it fails, you will be given a warning to run npm install , just in case you've forgotten.

What is the difference between npm start and npm run build?

npm install installs dependencies into the node_modules/ directory, for the node project you're working on. You can call install on another node. js project (module), to install it as a dependency for your project. npm run build does nothing unless you specify what "build" does in your package.


1 Answers

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"

like image 124
Michael Avatar answered Nov 15 '22 15:11

Michael