0

I'm trying to work out what the correct process for deploying a nodejs application to a production environment should me, using webpack.

If this was a standard Java/Maven project I might do something like:

mvn clean install -Pdeploy-production

where the mvn install compiles the application, and the additional deploy-production profile uses a maven plugin to deploy it to our production environment and do whatever else.

I've been looking at tutorials like this one, or this one.

In the first tutorial, he suggests something like this:

"script": {
    "start": "babel-node server-es6.js"
    "build:server": "babel server-es6.js --out-file server.js"
    "build:client": "webpack -p --config webpack.prod.config.js --progress"
}

Where essentially he provides two build scripts, and then a command to start the server running.

However, this doesn't explain how I actually get the code on to the production server and start it running.

As far as I see I have two options:

  • On the production environment checkout the source code, and run the build scripts there and start a node process.
  • Run the production build scripts in my development environment and SCP the dist folder to the production environment, and start a process there.

Can you point me in the right direction about what the best way to do this is?

  • 1
    How do you define "best?" The answer to that will probably go a long way towards guiding you in the right direction. Note that your answer should be pragmatic, specific and requirements-driven, not merely a tautology like "best practice." – Robert Harvey Jun 26 '17 at 0:19
2

The deployment process depends on your node.js application code base. If you use ES6/TypeScript, then you should transpile the code into ES5 with a help of a babel/tsc. You don't need webpack for server applications.

Your production environment should execute the following flow:

  1. Check out the existing source code
  2. Run npm install
  3. Build/transpile sources if needed (e.g. using TypeScript/ES6); this can be included in package.json as a post-install script
  4. Run npm start

It is a good solution to have your dependencies and main build/run scripts in a package.json configuration file.

There are also some handy libraries for managing node.js processes on linux (e.g. https://github.com/Unitech/pm2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.