1

How do I write a script in my Parent Folder's package.json file so that when I run npm install it installs the node modules in each folder and npm start will go to each folder and run npm start

The Frontend and Backend folder both use npm start to start up and I want to somehow do the same in the parent folder to simultaneously start both

This is the file structure:

ParentFolder
├── package.json .     <--- npm install && npm start scripts
├── FrontEnd
│   ├── /node_modules
│   ├── package.json
│   └── index.js
├── Backend
│   ├── /node_modules
│   ├── package.json
│   ├── routes.js
│   └── server.js.js
kayq
  • 133
  • 2
  • 12

1 Answers1

2

Installing in two directories is easy with find

find ./*/* -maxdepth 1 -name package.json -execdir npm install \;

This looks in each directory for a package.json and executes npm install;

npm start becomes a bit harder. At least on Windows using Cygwin, I wanted to do:

npm --prefix ./FrontEnd start ./FrontEnd & npm --prefix ./Backend start ./Backend

But it wasn't actually running in the background like I expected and FrontEnd was the only one that actually started. Depending on your start script this could work for you.

Possible solutions for this could be concurrently or npm-run-all -p.

Nathan
  • 581
  • 2
  • 17
  • That didn't work, it just made 2 folders called Backend and Frontend in the same parent directory. Sorry if I wasn't clear but what I intended to was that the start scripts will navigate to Frontend folder and run `npm install` then navigate to the Backend folder and run `npm install` – kayq Mar 07 '19 at 04:28
  • Ah thanks. I think I have a solution. Just running it locally to make sure it works. – Nathan Mar 07 '19 at 04:47
  • Edited with more info – Nathan Mar 07 '19 at 05:21