Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

npm

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'", } 
like image 917
fegemo Avatar asked Sep 25 '15 13:09

fegemo


People also ask

In which folder should I run npm install?

You should run it in your project root folder, or the folder above your node_modules folder as sometimes the structure can differentiate between projects.

What is npm run Prebuild?

A command line tool for easily making prebuilt binaries for multiple versions of node, electron or node-webkit on a specific platform. NPM.


2 Answers

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 
like image 22
x-magix Avatar answered Oct 11 '22 11:10

x-magix


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)

like image 86
Thomas Sparber Avatar answered Oct 11 '22 11:10

Thomas Sparber