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.
Related
I have two yarn workspaces A and B.
Within each workspace, I can run yarn start to run webpack dev server on different ports.
Therefore, I am trying to use the command yarn workspaces run start to start dev servers in both workspace A and B.
However, it only runs yarn start in workspace A and leaves the session open. My guess is it can't run yarn start for workspace B until the previous command ends.
Is there another simple way I can go about achieving this? I haven't been able to find any examples of this.
I don't think workspaces supports this. But lerna exposes a --parallel flag you can pass in that will do this.
One way to accomplish this is to install the concurrently package and in your package.json scripts do something like:
...
"start": "concurrently \"yarn start:A\" \"yarn start:B\",
"start:A": "cd A; yarn start",
"start:B": "cd B; yarn start",
...
https://www.npmjs.com/package/concurrently
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
What does yarn build command do ?
Are yarn build and npm build the same? If not what's the difference?
yarn build and npm build are not existing commands by default. I think you mean yarn run build or npm run build.
build is a command which can be specified in your package.json file on the scripts property. See the example below.
{
"name": "mypackage",
"version": "0.1.0",
"scripts": {
"build": "webpack --config webpack.dev.js"
}
}
In this example, build is a shortcut for launching command webpack --config webpack.dev.js. You can use every keyword you want to define some shortcuts to launch commands.
And the only difference between the two commands it's the JS dependency manager you're using, yarn or npm.
More infos :
https://yarnpkg.com/lang/en/
https://www.npmjs.com/
"yarn build
Bundles the app into static files for production." from Create React App by MAD9135.
https://mad9135.github.io/F2020/modules/week3/02-react-tooling/07-create-react-app.html
yarn build is a shortcut of yarn run build
from original documentation:
Reference: https://classic.yarnpkg.com/lang/en/docs/cli/run/#toc-yarn-run-script
its the same i guess the real difference between yarn and npm is the performance and security that yarn provides.
yarn actually installing packages in parallelly and Npm only install one package at a time
And Yarn have more secure dependency.
Yarn is a package manager for your code. Yarn build makes a bundle of apps in one format and shows errors if the app has any problem that will make an error on the server.
I got a private bitbucket repo A that I install via npm in my project B.
npm install git+ssh://git#bitbucket.org....git
That works with no problems.
But now I would like to run a build in A after installing it.
npm in default comes with a lot of scripts for stuff like that https://docs.npmjs.com/misc/scripts
I tryed postinstall, prepare, prepublish, preinstall in my package.json in A:
...
"scripts": {
"prepublish": "npm run build",
"build": "...",
...
On installing my package A in B I get npm Error: npm ERR! premature close
I would like to run the build on install to remove build files from git (A).
In this case the build runs webpack + babel compile.
Project B is made with create-react-app.
I don't want to eject create-react-app, setup webpack or compile all node_modules packages.
Any experience with this workflow?
There is no need to eject Project B just adding your Project A as a dependency in package.json is enough. And for Project A please use "preinstall" it will run before every npm install including when you run npm install on Project B. And in my case I just tested it it working perfectly in my machine. If you encountering issue I think it might be because of the way you building it maybe? So can you show us the build script?
Use prepare since according to the NPM docs it will install dependencies and devDependencies before the package is packaged and installed.
Which is helpful if you are compiling using Typescript, or doing transforms.
...
"scripts": {
"prepare": "npm run build",
"build": "...",
...
Note: If your dist/build directory is ignored in .gitignore. Add an empty .npmignore file to your repository; otherwise the prepare script won't build the directory.
Ref: https://docs.npmjs.com/cli/install
If I run yarn add <dependency> -D, yarn will install the dependency AND run a task in my package.json. It'll run the "start" script.
At first I was annoyed so I renamed "start" with something else, but now it'll run "build" after the add command...
Is there a way to prevent yarn from running any command after add ?
Here's my current scripts in package.json :
"scripts": {
"clean": "mkdir -p lib && rm -r lib",
"build": "yarn clean && tsc",
"test": "tsc && mocha -c",
"test:watch": "tsc-then -- mocha -c"
}
note: I opened an issue for those who are interested, as it seems to be specific to watchy^0.9.0 #atom/watcher problem #20
You have the --ignore-scripts flag.
From yarn add --help:
--ignore-scripts don't run lifecycle scripts
yarn add --ignore-scripts -D package-name-1 ...
You can find this, and more with yarn add --help.
This is on Yarn 1.7.0.
I've been having this issue as well since I switched to Yarn over NPM. It appears that yarn, when given an unknown command, searches for one with the same name in your package.json scripts.
Additionally, for some reason yarn runs an excessive amount of additional lifecycle scripts on top of ones specified here on npm.
I had three scripts; build, update, and prepare. Renaming build to babel stopped that from being run. Renaming update to submodules stopped that. And renaming prepare to update-and-transpile stopped it.
So... just pick a more convoluted name for the time being and it stops being run.
I realize this isn't exactly an answer to your question, I'm going to file an issue on their GitHub about disabling this functionality.