NodeJS package running multiple scripts when starting

yarn run multiple scripts sequentially
run multiple npm scripts in sequence
npm run-all
npm concurrently
npm-run-all vs concurrently
npm-run-all example
npm run-script
npm run scripts in series

I'm using NodeJS and I want to deploy my app.

When the app is on the server it to runs node ./bin/www (standard configured by express) in package.json, to start the server. But I want to run another script after the node ./bin/www script is done ./api.js (this script will loop forever).

The ./api.js is located in the root folder of the projecr

This is what i tried but no succes so far.

  "scripts": {
    "start": "node ./bin/www",
    "start": "node ./api.js"

The server launches both scripts, (checked by logs). But i cant access the website


    > x@ start /app
    > node ./api.js

    State changed from starting to crashed
    State changed from crashed to starting
    Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
    Stopping process with SIGKILL


#!/usr/bin/env node

 * Module dependencies.

var app = require('../app');
var debug = require('debug')('expressapp:server');
var http = require('http');

 * Get port from environment and store in Express.

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

 * Create HTTP server.

var server = http.createServer(app);

 * Listen on provided port, on all network interfaces.

server.on('error', onError);
server.on('listening', onListening);

 * Normalize a port into a number, string, or false.

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;

  if (port >= 0) {
    // port number
    return port;

  return false;

 * Event listener for HTTP server "error" event.

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      throw error;

 * Event listener for HTTP server "listening" event.

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);

The easiest way to do this would be to use the bash && operator, which runs one command then another once it is finished:

"scripts": {
  "start": "node ./bin/www && node ./api.js"

How can I run multiple npm scripts in parallel?, Then simple npm start (and npm run dev ) will work on all platforms! If you replace the double ampersand with a single ampersand, the scripts will run concurrently. then scripts/install.js will be called for the install and post-install stages of the lifecycle, and scripts/uninstall.js will be called when the package is uninstalled. . Since scripts/install.js is running for two different phases, it would be wise in this case to look at the npm_lifecycle_event environment var

NPM has pre and post hooks in the scripts section of package.json to put scripts execution in sequence.

So, you can do:

"scripts": {
    "start": "node ./bin/www",
    "poststart": "node ./api.js"

to get what you whant. This solution is fully supported across systems.

Happy coding!

npm Scripts: Tips Everyone Should Know, If you have one script that runs multiple commands, let's say CSS to npm-run- all lint:* , which would then match all scripts starting with the lint:� Most people are aware that is is possible to define scripts in package.json which can be run with npm start or npm test, but npm scripts can do a lot more than simply start servers and run tests. Here is a typical package.json configuration. // package.json // Define start and test targets { "name": "death-clock", […]

Since your ./bin/www/ is a long-running process(An Express app that listens for TCP connections) and there is won't be an "end" for a typical long-running job.

I think the best way you could achieve such is to include you ./api.js as a regular dependency

// ./bin/www

const app = express();


How to run more than one command as part of a npm script – Corey , A common scenario: as part of your npm start script, you need to have more than Rather than needing to have multiple scripts run, in multiple terminal/shell� A common scenario: as part of your npm start script, you need to have more than one command run (like webpack --config webpack.server.js and webpack --config webpack.client.js). Up until now you might have only run one command per script – often npm start just does something like node server.js .

How to run multiple npm scripts in parallel, This would mean that our instructors would have to start each project, run the test and record their score. This is a very a very tedious workflow� $ npm install express@4.8.0 express@4.8.0 added 36 packages from 24 contributors and audited 123 packages in 2.224s found 21 vulnerabilities (8 low, 9 moderate, 4 high) run `npm audit fix` to fix

How to run multiple npm scripts in parallel?, In my package.json I have these two scripts: "scripts": { "start-watch": "nodemon run-babel index. wp-server. How can I run these in parallel? npm ERR! missing script: start Solution: You need to tell npm what to do when you run npm start explicitly by editing package.json. First, identify the main file of your application. Most often it is called index.js, server.js or app.js. When you open package.json in an editor, you can also often find a line like "main": "index.js",

alisabrigok/run-multiple-npm-scripts: Run Multiple Different , Run Multiple Different Npm Scripts - Or Whatever You Want - At Once With A Single npm start. While some others aren't that much straightforward and require� To run the echo script, run npm run echo from the command line. Arbitrary scripts, such as echo above, have to be be run with npm run <script name>. npm also has a number of official scripts that it runs at certain stages of the package's life (like preinstall). See here for the entire overview of how npm handles script fields.

  • The logs don't show the server launching both scripts. They show the server launching node ./api.js and not node ./bin/www at all. I don't even think having multiple properties with the same key is valid JSON, so I'm surprised npm can read the package at all.
  • you were right. I have tried @ranieribt his answer and now it is running bin/www. But still not api.js, how can i solve this problem?
  • Thank you for the answer. I already tried that, it didnt work I think it is because './bin/www' listens connections to ports and is never finished?
  • Thank you for the answer, I tried It and it doesnt run I got back from the logs 'node ./bin/www' this probably means that it runs, the other script doesnt run. Maybe because /bin/www is never finished because it is listening to connections from ports?
  • You got the point. Try NPM Concurrently
  • Y've used Concurrently with no problem in an Express + Create React App setup.
  • Thank you for clearance on the never ending process. But since the script api.js is also a "never ending process" (its a loop). How can I implement this is bin/www. I have edited my question it now contains the sourcecode of www. I hope you can help me solve this problem
  • @AttackTheWar Perhaps you could add the require('./api') right after at line server.on('listening', onListening); so that long-running loop will kick start as soon as Express stares listening for traffic