Heroku: No such file or directory after build - express

I am trying to deploy a new app to Heroku using the build process on Heroku (it picks up the code changes from github and builds automatically). The client folders and package.json are in the root folder and the express files (including a separate package.json) are under /server.
{"error":"ENOENT: no such file or directory, stat '/app/build/index.html'"}
While troubleshooting this issue I launched bash and did a "dir" inside of the app directory. Sure enough, no "build" folder in root. I had overhauled a few things to separate the server's build from the main build - could I have screwed something up? Am I wrong in my understanding of what happens on Heroku?
Here is the package.json in root:
"scripts": {
"start": "node server/index.js",
"build": "react-scripts build",
"dev": "SET NODE_ENV=development&& node server/index.js",
"test": "SET NODE_ENV=test&& nodemon --exec mocha --recursive",
"heroku-postbuild": "cd server && npm install && npm run build"
EDIT
Not sure if something else is going on here. I went into root on the server and issued "npm run-script build" and the build folder is now there and I can see index.html there. However, same error persists.

Closing this as I had a more fundamental issue

Related

npm run serve vs build

In my Vue JS application I have a file called .env.individual which defines a variable use for making API calls to the backend.
I also have .env and .env.production, etc, all with different values for the API URL variable.
When I run npm run serve -- --mode individual the application starts up and uses the URL found in the .env.individual file. Likewise, when I run npm run serve -- --mode production the application starts up and uses the variable found in the .env.production file.
Given the above I was assuming that when I run npm run build -- --mode individual the \dist would be generated and I could then run npm run serve and the application would use the variables found in the .env.individual file.
Given my package.json file contains this:
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"deploy": "vue-cli-service s3-deploy",
"release": "npm run build && npm run deploy"
},
What is npm run serve actually doing and why - when I want to use a specific .env.XXX file do I need to specify it exactly?
npm run serve does not run your application from /dist folder. It compiles unoptimized build in memory (RAM). If you want run your optimized build from /dist folder, you can run it by some http server. For example https://www.npmjs.com/package/http-server .

Cannot install Tailwind on Windows Desktop

I've sent a program to my coworker so he can run it on his own machine, and when installing the dependencies and attempting "npm start", I'm given the error below.
> TAILWIND_MODE=watch craco start
'TAILWIND_MODE' is not recognized as an internal or external command,
operable program or batch file
I also got this error on my mac months ago, the easy fix was
npm install craco
however, I have installed craco and every other natural dependency and still get this error. Is there a compatibility issue with windows or something?
Try to put env before your command inside package.json script file:
> "env TAILWIND_MODE=watch craco start"
for example:
{
...
"scripts": {
"watch": "env TAILWIND_MODE=watch craco start",

How does npm run build work with npm pack?

I'm working on a project that requires using the npm pack command. Is running build on the library/package required before creating the installable tar file? Or can I just pack and then install it into the app?
Yes, but for totally different reasons.
npm run build can do anything: it just executes the value for build inside the scripts object in your package.json, for example on an Angular project it'll look like this,
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
npm pack basically "create a tarball from a package", but here is the deal: the "package" that it creates a tarball from has to exist. So, in order to create the "package" that npm pack targets in ./dist
you frequently have to run npm build. This is because many JavaScript projects are written in TypeScript (in which case "build" usually runs npx tsc) or use a build system that will transpile their code to produce JavaScript (like babel). If your project is very rudimentary, there is a good chance you won't have to "build anything": you can see this if you create an empty directory and run npm init, but seldom are useful packages created this way.
The result of npm pack is a .tgz file. This is basically a compressed copy of a target directory. You can install this with npm i ./file.tgz. On the consuming machine that you wish to install the package on, you do not have to build anything. Building is what you do as a developer and a package maintainer. For example, you build TypeScript producing JavaScript. The consumer just downloads the result and installs the dependencies. It doesn't even have to know that you write your code with TypeScript (or better, Rust.)

How to successfully deploy my Vue app? (CLI3)

This answer states that npm run build creates a dist folder (it does in my case) as well as an index.html file - it doesn't in my case.
Instead I have an index.html file inside my dist folder, and I upload this folder to my remote server, visiting that index.html in my browser displays a blank page with "Hello world" on top (no CSS, no JS).
I couldn't find anything addressing this issue in the docs.
Am I missing something? Why is my app reduced to a mere "Hello world" page once deployed?
If you are using vue-cli, to create dist folder you need to run npm run build.
Also , open your package.json and check your scripts objects. It will have various commands to run and you can chose the correct script to run.
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
For vue cli3, mostly you need to run npm run build.

Create-React-App deployment to Heroku failed with ` react-scripts: not found`

We're developing a ReactJS application using Create-React-App, that is served from our Node/Express server that also serves API's. We deploy the whole server to Heroku using node/JS buildpack and trying to get the CRA build step npm run build in postinstall script from the node package.json file as suggested by #mars in this issue.
The issue is that Heroku deployment is failing with this error. Note that this error happen to me sometime locally but then a npm install from the web_app is solving the issue, but not when run in Heroku. I have two related questions:
How to deploy to Heroku a Node/Express app that serves both APIs and a Create-React-App application? I can commit my build directory but this is really not the right way.
Why the react-scripts are disappearing and I have to run multiple times the npm install.
#johnnycon -
This was exactly the issue and I've received the answer from Mars in this github issue post:
#philjoseph, react-scripts is (by default) a devDependency for CRA apps, but Heroku Node buildpack has environment NODE_ENV=production which causes npm install to skip devDeps.
To install those devDeps too:
npm install --only=dev && npm install && npm run build
He also pointed to this excellent repo: https://github.com/mars/heroku-cra-node
I followed this and it works like a charm :)
Since you need "react-scripts" both in development and in production, you can simple move "react-scripts": "1.0.16" from "devDependencies" into "dependencies", so heroku doesn't ignore it.
There's a very simple solution. Before running the start script, Heroku will run a build script if it's in your package.json.
"scripts": {
"start": "node server.js",
"build": "cd client/ && yarn install && yarn build"
}
For Anyone in 2021, Follow Alexander Cherednichenko's VERY SIMPLE advice
Explanation:
As of October 2021, Create-React-App's react-script package has a ton of security vulnerability issues. I'd imagine a few people may run into their "fix" which suggests moving react-scripts to devDependencies. If you're deploying to Heroku this will NOT work. Moving react-scripts to devDependencies will cause this error since Heroku needs react-scripts to build the React app. It'll be fine locally, and you'll be vulnerability free when you run npm audit --production or yarn audit --production. Ultimately, though, Heroku will demand react-scripts is in the regular dependencies section.
The highest voted answer provides a great example repo by Mars Hall (a principal engineer at Heroku) for creating a react app that's served from a node backend. These days, though, it too includes react-scripts in the regular dependencies section of it's react-ui/package.json file.
In addition, since Create-React-App defaults to Yarn these days, the addition of npm install --only=dev will not run nor work (that I could tell based on the node buildpack log). Moreover, it may add testing dependencies that your app definitely won't need in production.
Unfortunately, until Create-React-App decides to do some updates, it's best to just ignore the issues that npm audit brings up (at least the ones related to react-scripts). Since react-scripts is a build tool, it's extremely unlikely for any vulnerabilities raised to be exploited.
P.S. This likely would be better suited as a comment under Alexander Cherednichenko's answer but I was one reputation away from being able to do so.
P.P.S Hopefully someone actually finds this helpful!
Are react-scripts declared as a devDependency or regular dependency in package.json? Since you're building on Heroku and heroku is reading the env variable as production (I'm assuming here), it won't install react-scripts. Try moving react-scripts as a regular dependency and try again.
With other cloud providers, I likely wouldn't follow this path, but with Heroku, it's the path of least resistance I've found. This article goes into a little more detail on your scenario. https://originmaster.com/running-create-react-app-and-express-crae-on-heroku-c39a39fe7851
i was trying exactly the same thing, deploy a create-react-app project to heroku, and was getting react-scripts not found something....
Heroku reads the scripts into package.json and cannot execute them.
The key to overcome this issue is to 'create/publish' the 'scripts' folder, which by default create-react-app does not create it, just to keep things simple.
So by running the eject command $npm run eject, the scripts folder its created as long as the config folder.
After that you can execute again the heroku script e.g. $ git push heroku master
and hopefully it wil deploy everything on Heroku.
More info about eject script on create-react-app documentation
Hope this helps, good luck.
Can you share how your Package.json file Scripts look? I actually deployed to Heroku yesterday and it seems to be working quite ok for me.
Maybe your dependencies are not added correctly in your package.json.
This is how my scripts look (if you don't have any scss/less/sass ignore the first 2 scripts):
"scripts": {
"build-css": "node-sass src/ -o src/",
"watch-css": "npm run build-css && node-sass src/ -o src/ --watch --recursive",
"start": "npm run start:server",
"watch": "npm-run-all --parallel watch-css start:*",
"start:server": "node server/server.js",
"start:client": "react-scripts start",
"build": "npm run build-css && react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"postinstall": "npm run build"}
When running in Development to take advantage of the Hotrealoding I run
npm run watch
And for PROD I run
npm run build
And then I deploy to Heroku. Try to check the logs from Heroku in a separate Terminal window so you can check what is wrong with your deployment.
Simply use heroku logs to display the last 100 lines of your logs.
Or to tail the logs in real-time: heroku logs -t.
Specify Node Environment
Tell heroku what version of heroku we want to use. By default heroku uses older version which will crash our app. you have 2 package.json files, one in client, another one is in the express server side. in express server side add this to package.json
"engines": {
"node": "12.9.1",
"npm": "6.10.2" },
Specify start script
Instruct heroku what command it should run to start up our server. Add this to the server side package.json
"start":"node index.js",
INSTALL HEROKU CLI
Install Heroku Cli
Sudo apt-get update
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh or sudo snap install heroku
In macos
brew install heroku cli
By default heroku uses a git based deployment procedure or workflow.
Git init
Git add .
Git commit -m “initial commit”
Heroku login
Heroku create nameOfTheApp
https://git.heroku.com/nameOfTheApp.git
This link is our deployment target. It is a git repository that we can push our local server to it.
Git remote add heroku https://git.heroku.com/nameOfTheApp.git
Git push heroku master
I had the same problem and happened to solve the problem by adding a few lines in the package.json (the one for the main project not the one in the client side) after seing #Daniel's comment.
What's happenning here is that Heroku is searching for the react scripts that can be used to install the dependecies and build the app on the client side. In my case both of those scripts were missing. In fact this is how my scripts looked like when I had the problem :
...
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"build": "cd client && npm run build",
"client": "cd client && npm start"
},
...
Heroku was expecting the scripts "install-client" and "heroku-postbuild". I just added them and the error dissapeared :
...
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"build": "cd client && npm run build",
"client": "cd client && npm start",
"install-client": "cd client && npm install",
"heroku-postbuild": "npm run install-client && npm run build"
},
...
I hope this would help someone.