I have a repository which contains a backend (Node/Express) and a Frontend client, as such:
├── build
├── config
├── coverage
│ └── lcov-report
├── dist
│ └── static
├── server (node/express server)
│ ├── coverage
│ ├── docs
| ├── src
│ ├── etc
│ └── package.json
|
├── src (Vue.js : client code)
│ ├── api
│ ├── assets
│ ├── components
│ ├── router
│ └── store
└── static
└── package.json
I have two package.json files, one for the client and one for the server.
Which would be the advantage and disadvantages of having both frontend and backend parts in the same package.json?
You can use heroku-postbuild and maintain separate package.json files for your client and server in a single git repo that you push to Heroku.
For example, in one of my projects, the directory structure looks like this:
|-- package.json (for node/express server)
|-- Procfile
|-- www
|--client
|-- package.json (for Ionic/Angular client app)
|-- ...
|--server
|--- ...
|-- server.js (top level node.js/express script for server)
In my top-level package.json, I have:
"scripts": {
"start": "node www/server.js",
"heroku-postbuild": "cd www/client && npm install && npm run build"
},
In my client package.json I have:
"scripts": {
"build": "ionic-app-scripts build",
...
},
And finally in my Procfile I have:
web: npm start
With this solution, Heroku runs my server and builds my client code on every Heroku build.
I think client and server package.jsons should be kept separate for several reasons. For one thing, you really don't want all your server-side code bundled into your client.
I had a similar problem deploying to heroku. I use a package called concurrently to start client side and server side via just the start script in the server side package.json. I also use node's built in proxy feature to send any requests from the client to the server by adding a line to the client package.json.
By the way, I use create-react-app for the client side so thats why some stuff looks a little strange.
My folder structure is
Server folder
Server package.json
Client folder
Client package.json
Server package.json:
"scripts": {
"start": "concurrently \"npm run server\" \"npm run client\"",
"server": "cross-env NODE_ENV=production node server.js",
"server-dev": "nodemon --watch ./ --exec babel-node -- server.js",
"client": "node start-client.js",
"dev": "concurrently \"npm run server-dev\" \"npm run client\"",
"lint": "eslint ."
},
Client package.json:
"proxy": "http://localhost:3001",
I assume Heroku just looks for a start script and runs that. I think having some degree of separation between your server and client is a good idea so I wouldn't recommend trying to fit it all in one package.json
If you want you could probably find a tutorial online by googling with keywords like: heroku concurrently server client
btw, you don't need CORS if you set up like this
Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With