The compiled code will be output to a dist directory in the project root. Between each build, we need to remove it so we are in a clean state. To do that in a cross-platform manner, we will use the rimraf package that can recursively delete files and folders.
- Install the rimraf package to the dev dependencies:
npm i -D rimraf
- Add a build script for both the client and server bundles:
"build:client": "cross-env NODE_ENV=production webpack --progress
--hide-modules --config webpack/client.js",
"build:server": "cross-env NODE_ENV=production webpack --progress
--hide-modules --config webpack/server.js",
We set the NODE_ENV environment variable to 'production' and run the webpack command with the corresponding webpack configuration file.
- Create a new build script that clears the dist folder and runs the two other build:client and build:server scripts:
"build": "rimraf dist && npm run build:client && npm run
build:server",
- Add a last script called start that runs the express server in production mode:
"start": "cross-env NODE_ENV=production node server",
- You can now run the build; use the usual npm run command:
npm run build
The dist folder should now contain all the chunks generated by webpack, plus the server bundle and client manifest json files:
These are the files that need to be uploaded to your real nodejs server.
- We can now start the express server:
npm start
You should also upload the server.js, package.json, and package-lock.json files to the real server. Don't forget to install the dependencies with npm install.