The Gandi Community

Node.js: Start script options, ES6, and process management

Simple Hosting, our Platform-as-a-Service hosting option, now supports npm start scripts, a function currently used by nearly all pre-packaged Node.JS applications that also promises to offer greater functionality to advanced Simple Hosting users.

Until now, you needed to put a ‘server.js’ file on the root of your project in order for your application to boot correctly. Now you have added flexibility and control, specifically over the way your application starts up using a `package.json` file.

You can, then, define the point of entry of your application with `package.json[“main”]` :

//package.json
{
"name" : "foo",
"version" : "1.2.3",
"description" : "A packaged foo fooer for fooing foos",
"main" : "index.js",
}

In the above example, the index.js file is used to boot your application with node.

The other option is to define a command to launch on start-up of your application with `package.json[“scripts”][“start”]`

// package.json
{
"name" : "foo",
"version" : "1.2.3",
"description" : "A packaged foo fooer for fooing foos",
"scripts": {
"start": "node index.js"
}
}

Careful readers may have already discerned, correctly, that this means access to the launch parameters of the node process. This means, for example, that you can use ES6, the newest version of JavaScript, and all the latest features that entails, on your instance

//package.json
{
"engines": {
"node": ">=0.12"
},
"scripts": {
"start": "node --harmony app.js"
}
}

This new functionality allows you also to take environment variables into account with the start command. Previously, this was only possible from the application itself.

// package.json
{
...
"scripts": {
"start": "NODE_ENV=staging node app.js"
}
}

You can use this functionality to manage your application’s processes yourself, with process managers like pm2.

We recommend taking a look at our wiki to see more examples of both basic and advanced uses.

If you already have a Node.js instance, this update doesn’t require any change. To take advantage of the new functionalities, you just need to delete or rename `server.js` and configure `package.json` to start up according to your customization.

Of course, for your next project, we recommend starting off using these new features.

These changes will become permanent on December 1, 2015 when priority will be given to instructions found in the `package.json` file. The file `server.js` will only be referenced from that point on as a last resort.

Want to try Node.js on a Simple Hosting instance? Try a free five-day trial.