Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chaining package.json scripts to start Express server and Vue app

I have built an app using Vue.js and express.js. Currently I have to open two terminal windows and run npm run serve in one and npm start in the other. I want to make the server run after the Vue.js app builds in the same terminal. I read on this article that I can get both scripts to run by chaining the two package.json scripts together, but for the life of me can't figure out how. My project is structured as such:

├── project
├── _frontend
|   ├── package.json
├── backend
|   ├── package.json

I have tried the following ways:

1st try - "serve": "vue-cli-service serve && (cd ../server && npm start)"
2nd try - "serve": "vue-cli-service serve && (cd ../server && npm run start)"
3rd try - "serve": "vue-cli-service serve && (cd ../server) && npm start"

The Vue.js app builds and runs just fine but the server does not start. I tried doing the reverse on the server package.json as well and the server starts but the app does not build. Is this something I can not accomplish due to the folder setup or what am I doing wrong?

like image 375
jaronow Avatar asked Aug 18 '19 05:08

jaronow


1 Answers

&& executes commands in series. vue-cli-service serve && cd ../server && npm start won't work as expected because the script stops at vue-cli-service serve until the server is shut down.

For cross-platform script, concurrently or other similar packages can be used to execute commands in parallel:

"serve": "vue-cli-service serve",
"start": "concurrently \"npm run serve\" \"cd ../server && npm start\""
like image 145
Estus Flask Avatar answered Nov 28 '22 05:11

Estus Flask