error while pushing node js project to heroku - node.js

I am getting this error while I am trying to push my project to heroku. Backend is writtend in nodeJS, frontend in React. I think the problem is heroku-postbuild, because if I remove it, push works. I was searching for it but still I have no idea what is the reason. Can somebody help me?
/tmp/build_7504975f08f330415cdefb27437cfad9/node_modules/.bin/node: 1: /tmp/build_7504975f08f330415cdefb27437cfad9/node_modules/.bin/node: MZ����#: not found
/tmp/build_7504975f08f330415cdefb27437cfad9/node_modules/.bin/node: 1: /tmp/build_7504975f08f330415cdefb27437cfad9/node_modules/.bin/node: Syntax error: word unexpected (expecting ")")
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! project#1.0.0 heroku-postbuild: `cd client && npm install && npm run build`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the project#1.0.0 heroku-postbuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /tmp/npmcache.W89m8/_logs/2018-11-13T21_37_22_676Z-debug.log
-----> Build failed
We're sorry this build is failing! You can troubleshoot common issues here:
https://devcenter.heroku.com/articles/troubleshooting-node-deploys
Some possible problems:
- node_modules checked into source control
https://blog.heroku.com/node-habits-2016#9-only-git-the-important-bits
- Node version not specified in package.json
https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version
Love,
Heroku
! Push rejected, failed to compile Node.js app.
! Push failed
package.json (server)
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js",
"server": "nodemon server.js",
"client-install": "npm install --prefix client",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "cd client && npm install && npm run build"
},
"author": "",
"license": "ISC",
"dependencies": {
"concurrently": "^4.0.1",
"express": "^4.16.4",
"jsonwebtoken": "^8.3.0",
"mongoose": "^5.3.4",
"node": "^8.10.0",
"nodemon": "^1.18.4",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"react-scripts": "1.0.11",
},
"devDependencies": {
"nodemon": "^1.18.4",
"react-scripts": "1.0.11"
}
}
package.json (client)
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"parallelshell": "^3.0.2",
"react-dom": "^16.5.2",
"react-redux": "^5.0.7",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-router-redux": "^4.0.8",
"react-scripts": "2.0.5",
"socket.io-client": "^1.7.3",
},
"scripts": {
"start": "parallelshell \"react-scripts start\" \"sass --watch src/styles/scss:src/styles/css --style compressed\"",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"sass": "sass --watch src/styles/scss:src/styles/css --style compressed",
"sass-nw": "sass src/styles/scss:src/styles/css"
},
"proxy": "http://localhost:5000/",
}

I ran into the similar error while troubleshooting a project. I resolved it by using the suggestions from the prompt.
Some possible problems:
- node_modules checked into source control
https://blog.heroku.com/node-habits-2016#9-only-git-the-important-bits
- Node version not specified in package.json
https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version
So try do a "rm -rf node_modules" in the root directory if you checked node_modules into the repository. Heroku will install node_modules in the deployed environment and does not need your checked in version.

Related

Failed build on Heroku with custom heroku-postbuild script

I have two main folders for my project: server and client. Heroku only seems to care about the server/package.json. Deployment is going fine until it returns an error when the process runs the "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client" command from package.json.
Here's the error message from the console:
-----> Build
remote: Running heroku-postbuild
remote:
remote: > server#1.0.0 heroku-postbuild
remote: > npm install --prefix client && npm run build --prefix client
remote:
remote: npm ERR! code ENOENT
remote: npm ERR! syscall open
remote: npm ERR! path /tmp/build_25b1f86c/client/package.json
remote: npm ERR! errno -2
remote: npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_25b1f86c/client/package.json'
remote: npm ERR! enoent This is related to npm not being able to find a file.
remote: npm ERR! enoent
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR! /tmp/npmcache.3i4hV/_logs/2022-08-05T15_34_56_961Z-debug-0.log
remote:
remote: -----> Build failed
Here's server/package.json:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "16.16.0",
"npm": "8.11.0"
},
"scripts": {
"start": "node index.js",
"server": "nodemon index.js",
"client": "npm run start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.0",
"concurrently": "^7.3.0",
"cookie-session": "^2.0.0",
"express": "^4.18.1",
"mongoose": "^6.5.0",
"nodemon": "^2.0.19",
"passport": "^0.5.3",
"passport-google-oauth20": "^2.0.0",
"stripe": "^10.0.0"
}
}
Here's client/package.json:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"http-proxy-middleware": "^2.0.6",
"materialize-css": "^1.0.0-rc.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.2",
"react-router-dom": "^5.3.3",
"react-scripts": "5.0.1",
"react-stripe-checkout": "^2.6.3",
"redux": "^4.2.0",
"redux-thunk": "^2.4.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I tried removing parts of "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client" to shorten it into "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false" in other deployments and it worked. It seems "npm install --prefix client && npm run build --prefix client" is the part not working in the heroku-postbuild script.
Would you have any idea how to fix this? Thanks!
OK, found it. My course TA helped me figure it out. The solution below worked.
In my Github repo called server, the client directory was completely missing, so, the Heroku server was likely having the same problem. And it did. I had to delete the .git directory in the client so version control doesn't think this is a submodule. From the root repository:
cd client
rm -r .git
git rm --cached .
cd ..
git add client
git commit -m "remove submodule"
git push origin main
git push heroku main

ENOENT: no such file or directory, open 'C:\Users\username\package.json' when need to run docker and nodejs

i need to build image from nodejs project . but when i run this command :
docker run image-name
it show me this error :
C:\Users\username\package.json
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\username\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
this is my docker file :
FROM node:14.16.1
ENV NODE_ENV=production
WORKDIR /
COPY ["package.json", "package-lock.json*", "./"]
RUN npm cache clean --force && npm install
COPY . .
EXPOSE 3000
CMD npm run start
and this is package.json :
{
"name": "Profile",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "rimraf dist && tsc",
"preserve": "npm run build",
"serve": "cross-env NODE_ENV=development concurrently \"tsc --watch\" \"nodemon -q dist/index.js\"",
"prestart": "npm run build",
"start": "cross-env NODE_ENV=production node dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#types/body-parser": "^1.19.1",
"body-parser": "^1.19.0",
"cross-env": "^7.0.3",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"helmet": "^4.6.0",
"mongoose": "^5.13.7",
"rimraf": "^3.0.2"
},
"devDependencies": {
"#types/express": "^4.17.13",
"#types/mongoose": "^5.11.97",
"#types/node": "^16.7.1",
"concurrently": "^6.2.1",
"nodemon": "^2.0.12",
"ts-node": "^10.2.1",
"typescript": "^4.3.5"
}
}
whats the problem ?? how can i solve this problem ??
Edit
OS : Windows 10
Command For Build Image : docker build -tag profile-srv
Project Structure :
I guess:
FROM node:14.16.1
ENV NODE_ENV=production
WORKDIR /
COPY package.json .
COPY package-lock.json* .
RUN npm cache clean --force
RUN npm install
COPY . .
EXPOSE 3000
CMD npm run start

Push to Heroku being rejected, breaking in package.json

When trying to push to Heroku I keep getting this error:
remote: ! Push rejected to infinite-cove-33100.
remote:
To https://git.heroku.com/infinite-cove-33100.git
! [remote rejected] master -> master (pre-receive hook declined)
It always breaks at:
remote: > mern_ecommerce#1.0.0 heroku-postbuild /tmp/build_43ebd568
remote: > NPM_CONFIG_PRODUCTION=false npm install --prefix frontend && npm run build --prefix frontend
remote:
remote: up to date in 0.284s
remote: found 0 vulnerabilities
remote:
remote: npm ERR! code ENOENT
remote: npm ERR! syscall open
remote: npm ERR! path /tmp/build_43ebd568/frontend/package.json
remote: npm ERR! errno -2
remote: npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_43ebd568/frontend/package.json'
remote: npm ERR! enoent This is related to npm not being able to find a file.
All versions in the package.json are accurate, everything that's needed in .gitignore is there including node_modules, heroku has the right buildpack and everything has been added and committed with git.
I've been through: https://devcenter.heroku.com/articles/troubleshooting-node-deploys
and no solution yet.
This is the package.json where it keeps breaking:
{
"name": "mern_ecommerce",
"version": "1.0.0",
"engines": {"node": "14.15.1"},
"description": "MERN shopping cart app",
"main": "server.js",
"type": "module",
"scripts": {
"start": "node backend/server",
"server": "nodemon backend/server",
"client": "npm start --prefix frontend",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"data:import": "node backend/seeder",
"data:destroy": "node backend/seeder -d",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix frontend && npm run build --prefix frontend"
},
"author": "John Doe",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"colors": "^1.4.0",
"dotenv": "^8.2.0",
"e
xpress": "^4.17.1",
"express-async-handler": "^1.1.4",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.11.4",
"morgan": "^1.10.0",
"multer": "^1.4.2",
"node": "^14.15.1",
"react": "^17.0.1",
"react-scripts": "^4.0.1"
},
"devDependencies": {
"concurrently": "^5.3.0",
"nodemon": "^2.0.6"
}
}
I don't know what the original problem was but I was able to deploy by using the deploy tab on the Heroku site via linking github... not how I wanted to do it but it works.
For anyone having trouble with their Heroku builds, it might be worthwhile to install npx by running npm install -g npx and let it inspect the contents of your package.json file by running npx #heroku/update-node-build-script.
Also see: https://www.npmjs.com/package/#heroku/update-node-build-script
For manual inspection check: https://help.heroku.com/P5IMU3MP/heroku-node-js-build-script-change-faq
try running npm install sometimes it's happening because you manually updated your package.json, which will not affect the package-lock.json hence it's different from the remote one hence can not get deployed

Something wrong with my heroku deployment

`NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client`
remote: npm ERR! Exit status 254
remote: npm ERR!
remote: npm ERR! Failed at the walking-bucket#1.0.0 heroku-postbuild script.
This is the error I get when I try to deploy my express/React app to heroku, any ideas what is going wrong?
edit: package.json with build script:
{
"name": "walking-bucket",
"version": "1.0.0",
"description": "basketball shot journal log",
"main": "server.js",
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^3.0.2",
"bcrypt-nodejs": "0.0.3",
"body-parser": "^1.18.3",
"concurrently": "^4.1.0",
"express": "^4.16.4",
"express-session": "^1.15.6",
"mongoose": "^5.3.14",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^5.0.1"
},
"devDependencies": {
"nodemon": "^1.18.7"
}
}
Got my solution with the build command
"build": "react-scripts build"
from the create-react-app package.json

Node/React app won't deploy to Heroku

My app has a React frontend and a Node backend; the BE serves the FE data through an API. It won't deploy to Heroku—stack trace is below.
Things I've tried:
This question. I've tried modelling my package.json files after a model repo mentioned in the answer, but no luck.
Deploying the model repo mentioned above—it worked fine.
Deploying the model repo to my own failing heroku app—it worked fine, so Heroku isn't the issue.
This question (I've checked react-scripts is listed as a dependency and not a dev-dependency).
I will probably end up taking the model repo as a starting point and gradually copying my project over, but would appreciate any info on what might be going wrong.
Stack Trace:
> fsevents#1.0.17 install /tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client/node_modules/fsevents
> node-pre-gyp install --fallback-to-build
node-pre-gyp ERR! Tried to download(404): https://fsevents-binaries.s3-us-west-2.amazonaws.com/v1.0.17/fse-v1.0.17-node-v48-linux-x64.tar.gz
node-pre-gyp ERR! Pre-built binaries not found for fsevents#1.0.17 and node#6.10.3 (node-v48 ABI) (falling back to source compile with node-gyp)
make: Entering directory '/tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client/node_modules/fsevents/build'
SOLINK_MODULE(target) Release/obj.target/.node
COPY Release/.node
make: Leaving directory '/tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client/node_modules/fsevents/build'
> fibers#1.0.15 install /tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client/node_modules/fibers
> node build.js || nodejs build.js
`linux-x64-48` exists; testing
Binary is fine; exiting
added 1242 packages in 52.433s
removed 1242 packages in 25.561s
> react-ui#0.1.0 build /tmp/build_f03b382a2b768aeb89ff9c197145d0a5/client
> react-scripts build
sh: 1: react-scripts: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! react-ui#0.1.0 build: `react-scripts build`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the react-ui#0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
My server package.json:
{
"name": "in-search-of-happiness",
"engines": {
"node": "6.10.x"
},
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"heroku-postbuild": "cd client/ && npm install --only=dev && npm install && npm run build"
},
"cacheDirectories": [
"node_modules",
"client/node_modules"
],
"dependencies": {
"body-parser": "^1.17.2",
"cookie-parser": "^1.4.3",
"debug": "~2.6.3",
"express": "~4.15.2",
"jade": "^1.11.0",
"mongoose": "^4.10.8",
"morgan": "^1.8.2",
"serve-favicon": "^2.4.3",
"zombie": "^5.0.5"
}
}
My React package.json:
{
"name": "react-ui",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^3.3.7",
"fetch": "^1.1.0",
"node-fetch": "^1.7.1",
"process-nextick-args": "^1.0.7",
"react": "^15.6.1",
"react-bootstrap": "^0.31.0",
"react-dom": "^15.6.1",
"react-scripts": "1.0.7",
"util-deprecate": "^1.0.2"
},
"devDependencies": {
"chai": "^4.0.2",
"eslint": "^4.0.0",
"sinon": "^2.3.5",
"wdio-mocha-framework": "^0.5.10",
"webdriverio": "^4.8.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:3001"
}
Hello your post build script needs some modification, kindly follow this convention
Replace reactFolderName to your folder name that contains the react frontend
Add This below snippet to your package.json under the scripts
scripts{
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix reactFolderName && npm run build --prefix reactFolderName"
}
And add this snippet to your index.js file
app = express()
app.use(express.static('reactFolderName/build'));
app.get('*', (req, res) => res.sendFile(path.resolve(__dirname, 'reactFolderName', 'build', 'index.html')));

Resources