'How to deploy my app to heroku when i have two different folders
I have a project with two defferent folders. One is frontend the another is back-end. I want to deploy this both folders on one heroku app.
In the server.js i have
app.get('*', (req, res) => {
return res.sendFile(path.join(__dirname, '../frontend/dist/index.html'))
})
so i can serve the index.html file from angular.But when i try to deploy withing git bash on the root of the folder,with that two fodlers i get errors that the language is not specified or something.I googled the error and people say that is because i don't have package.json file at the root of the folder.
How can i have two different folders and two different package.json so i can make deploy to heroku, or i can't ? what is the solution ?
Solution 1:[1]
In your Node App index.js
file add below code & move your client code inside client directory
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (req, res)=>{
res.sendFile(path.join(__dirname, "client/build",'index.html'));
})
And in your Package.json
"scripts": {
"client": "cd client && yarn start",
"server": "nodemon server.js",
"build": "cd client && npm run build",
"dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\"",
"start": "node server.js",
"heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
}
Solution 2:[2]
The directory structure I find gives the least issues when deploying to Heroku is the following:
App (root)
|
|
+---client
| |
| +---...
|
+---models
+---routes
+---package.json (node)
+---Procfile
+---server.js (or index.js - definition dependent)
Your package.json (node) scripts should look something like this (keeping other custom scripts):
"scripts": {
"start": "nodemon server.js",
"heroku-postbuild": "npm install --prefix client && npm run build --prefix client"
}
Note: your client package.json scripts should remain relatively standard.
Procfile should be:
web: node server.js
...and as @Rahul Cv said, in your server.js (or index.js):
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (req, res)=>{
res.sendFile(path.join(__dirname, "client/build",'index.html'));
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rahul Cv |
Solution 2 | DharmanBot |