fegemo
fegemo

Reputation: 2594

Is it possible to use npm to run scripts in multiple subfolders?

I have a folder (a project) with 3 subfolders (client, server, assets). Each subfolder has a different command to start and to work on the project I need to start the 3 apps. This is the folder layout and the commands I use to start each subproject:

  • project
    • client (ionic serve)
    • server (node index)
    • assets (http-server -p 8082)

Currently, I go to each of the three folders and start each of the apps. To make the process more standard, each subproject has a package.json with a start script, so I just cd subfolder && npm start.

My question: is it possible to use npm on the parent folder (i.e., write a package.json there) in such a way that I can just run the following command and have the same (or similar) effect?

project> npm start

I have tried using the package parallelshell, but it didnt work (probably because of the cd:

"scripts": {
  "start": "parallelshell 'cd app && ionic serve' 'cd api && npm start' 'cd assets && npm start'",
}

Upvotes: 48

Views: 53342

Answers (3)

Thomas Sparber
Thomas Sparber

Reputation: 2917

You can use concurrently to accomplish this. So you would create a package.json which looks something like the following:

...
"scripts": {
  "client": "cd client && npm start",
  "server": "cd server && npm start",
  "assets": "cd assets && ionic serve",
  "start": "concurrently \"npm run client\" \"npm run server\" \"npm run assets\" ",
},
...
"devDependencies": {
  "concurrently": "^1.0.0"
}
...

Note: This will start all three processes concurrently which means that you get mixed output of all three (like @topheman already mentioned)

Upvotes: 88

x-magix
x-magix

Reputation: 2852

it is really late to answer but you got built-in option --prefix, example:

-package.json
-/dist/ssr/package.json
# package.json in root
npm run start --prefix dist/ssr

Upvotes: 71

topheman
topheman

Reputation: 7902

The problem is that all your three scripts are server launching-like script task, which means that they're not like a build task (for example) that runs for 10s and stop the process.

For each one of them, you launch them, and the process continues indefinitly.

You could launch all of them in a daemon way with something like forever, but in your case, you are in dev mode (so you want all the logs, and you don't want the errors from the nodejs server mixed with the ionic one ...).

In case you don't mind about mixing logs: https://www.npmjs.com/package/forever (I assume this does nearly the same thing as parallelshell ...)

Upvotes: 5

Related Questions