React/webpack application deploy fails on heroku - reactjs

I am trying to deploy my react starter on heroku. I have written the webpack file and it works on my localhost but as I deploy on heroku it throws a run time error of:
Cannot find module '../lib/util/addDevServerEntrypoints'
My stacktrace looks like
Error: Cannot find module '../lib/util/addDevServerEntrypoints'
2017-03-28T11:56:47.036328+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:469:15)
2017-03-28T11:56:47.036329+00:00 app[web.1]: at Function.Module._load (module.js:417:25)
2017-03-28T11:56:47.036329+00:00 app[web.1]: at Module.require (module.js:497:17)
2017-03-28T11:56:47.036330+00:00 app[web.1]: at require (internal/module.js:20:19)
2017-03-28T11:56:47.036330+00:00 app[web.1]: at Object. (/app/node_modules/webpack-dev-server/bin/webpack-dev-server.js:9:33)
2017-03-28T11:56:47.036331+00:00 app[web.1]: at Module._compile (module.js:570:32)
2017-03-28T11:56:47.036331+00:00 app[web.1]: at Object.Module._extensions..js (module.js:579:10)
2017-03-28T11:56:47.036332+00:00 app[web.1]: at Module.load (module.js:487:32)
2017-03-28T11:56:47.036333+00:00 app[web.1]: at tryModuleLoad (module.js:446:12)
2017-03-28T11:56:47.036333+00:00 app[web.1]: at Function.Module._load (module.js:438:3)

I have faced similar problems while deploying my app in heroku.
It's best to write a node express server that serves public folder to deploy your app rather than using webpack-dev-server.
Node express server can be easily created like following.
const express = require('express')
const path = require('path')
const app = express()
app.use(express.static(path.resolve(__dirname,'dist')
app.listen(process.env.PORT || 8080)

web-dev-server is meant to be used only in development mode. Use "serve" instead.
Do npm install serve --save or yarn add serve -D
Add the following scripts to your package.json
"scripts": {
"start": "serve -s dist",
"dev": "webpack-dev-server --mode development --inline --open --hot",
"build": "webpack --mode production",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Before deployment run, npm build, or yarn build for the Webpack to generate your static files. Then push normally to Heroku. Once the deployment build succeeds, Heroku runs the start script. This way it would use "serve" to serve you static files created in the dist folder. So you use Webpack-dev-server for dev purpose and "serve" in production. Either way serving the dist folder files through node also works. Cheers

Related

new gatsby project is not running

I've created new gatsby project. After successful installation, I tried to run it with command npm run develop and gatsby develop but it is throwing error. Unable to figure out the root cause of error. I tried re installing node_modules but nothing is working! Please Help! Thank you!
ERROR:
D:\FC\gatsby-shopify-site>npm run develop
> test-site#1.0.0 develop
> gatsby develop
D:\FC\gatsby-shopify-site\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:53
throw ex;
^
Error: The specified module could not be found.
\\?\D:\FC\gatsby-shopify-site\node_modules\#parcel\source-map\parcel_sourcemap_node\artifacts\index.win32-x64-msvc.node
at Object.Module._extensions..node (node:internal/modules/cjs/loader:1185:18)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (D:\FC\gatsby-shopify-site\node_modules\#parcel\source-map\parcel_sourcemap_node\index.js:15:18)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (D:\FC\gatsby-shopify-site\node_modules\#parcel\source-map\dist\node.js:14:18)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32) {
code: 'ERR_DLOPEN_FAILED'
}
package.json
{
"name": "test-site",
"version": "1.0.0",
"private": true,
"description": "test-site",
"keywords": [
"gatsby"
],
"scripts": {
"develop": "gatsby develop",
"start": "gatsby develop",
"build": "gatsby build",
"serve": "gatsby serve",
"clean": "gatsby clean"
},
"dependencies": {
"gatsby": "^4.9.0",
"react": "^17.0.1",
"react-dom": "^17.0.1"
}
}
gatsby-config.js
module.exports = {
siteMetadata: {
siteUrl: `https://www.yourdomain.tld`,
},
plugins: [
]
}
PLEASE HELP!!!
I have solved the issue by adding a library of Microsoft Visual C++ 2015 Redistribution on my computer as the package is failing due to a missing library.
The download link is here: https://aka.ms/vs/17/release/vc_redist.x64.exe
It took a lot of time for mine to solve. Hope that it will help you.
Where (in what folder) have you run npm install?
In D drive and inside FC folder
You must install your dependencies inside your project folder, so inside gatsby-shopify-site. In your case, you installed your dependencies in the parent folder (FC),
Move inside D:\FC\gatsby-shopify-site and run:
npm install
Once done:
gatsby develop
Make sure you installed the right node js version for your Windows system.
As mentioned in the error, it is trying to execute a win32-x64 version. In my case Node website recommended the wrong version when downloading and this was fixed by changing it.
Error: The specified module could not be found.
\\?\D:\FC\gatsby-shopify-site\node_modules\#parcel\source-map\parcel_sourcemap_node\artifacts\index.win32-x64-msvc.node
Could you try yarn instead?
npm install -g yarn
and then run your command like this
yarn run develop

Node.js node_modules\grpc\src\node\extension_binary\node-v83-win32-x64-unknown\grpc_node.node

I was doing a proof of concept on gRPC using node.js
Here is my package.json
{
"name": "grpc-node-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"#grpc/proto-loader": "^0.1.0",
"grpc": "^1.11.0",
"lodash": "^4.6.1"
}
}
After writing in proto file, client and server files. When I run the command node server.js
I got the following error
PS C:\Learnings\node\grpc-nodejs-demo> node server.js
C:\Learnings\node\grpc-nodejs-demo\node_modules\grpc\src\grpc_extension.js:58
throw e;
^
Error: Failed to load C:\Learnings\node\grpc-nodejs-demo\node_modules\grpc\src\node\extension_binary\node-v83-win32-x64-unknown\grpc_node.node. \\?\C:\Learnings\node\grpc-nodejs-demo\node_modules\grpc\src\node\extension_binary\node-v83-win32-x64-unknown\grpc_node.node is not a valid Win32 application.
\\?\C:\Learnings\node\grpc-nodejs-demo\node_modules\grpc\src\node\extension_binary\node-v83-win32-x64-unknown\grpc_node.node
at Object.Module._extensions..node (internal/modules/cjs/loader.js:1122:18)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Learnings\node\grpc-nodejs-demo\node_modules\grpc\src\grpc_extension.js:32:13)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
PS C:\Learnings\node\grpc-nodejs-demo>
Any thoughts??
After some research, I did the following steps to resolve the issue
I was missing node-gyp - which is Node.js native addon build tool. It can be installed by
npm install -g node-gyp
Install Python
Install Visual C++ Build Environment
Clean up node-modules
Then do in the folder npm install
The following link talks the above steps in detail:
https://github.com/nodejs/node-gyp#on-windows

Yarn start - Command failed with exit code 1

I'm having a little problem while trying to run "Yarn start" in my ReactJS project.
The error is pasted below:
yarn run v1.22.5
$ webpack-dev-server --mode development
The CLI moved into a separate package: webpack-cli
Please install 'webpack-cli' in addition to webpack itself to use the CLI
-> When using npm: npm i -D webpack-cli
-> When using yarn: yarn add -D webpack-cli
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'webpack-cli/bin/config-yargs'
Require stack:
- C:\3AM\web\node_modules\webpack-dev-server\bin\webpack-dev-server.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\3AM\web\node_modules\webpack-dev-server\bin\webpack-dev-server.js:65:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\3AM\\web\\node_modules\\webpack-dev-server\\bin\\webpack-dev-server.js'
]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I've tried to install webpack-dev-server with npm and yarn, but it didn't solve.
In package.json change the start script to "start": "webpack serve".

Error: webpack-dev-server with ReactJS and Django

I try to create a full stack environment with Django + ReactJS. Here, I use Webpack bundler and I'm getting this error while I try to run my webpack-server from a virtualenv. I'm very new to the application development and this is my first attempt, So can anyone help me out?
PS C:\Users\NikhileshSubramanian\Desktop\eduWebApp\env\frontend> npm run dev
frontend#0.1.0 dev C:\Users\NikhileshSubramanian\Desktop\eduWebApp\env\frontend
webpack-dev-server
module.js:549
throw err;
^
Error: Cannot find module 'webpack/lib/node/NodeTemplatePlugin'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\NikhileshSubramanian\node_modules\html-webpack-plugin\lib\compiler.js:9:28)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! frontend#0.1.0 dev: `webpack-dev-server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the frontend#0.1.0 dev 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! C:\Users\NikhileshSubramanian\AppData\Roaming\npm-
cache\_logs\2018-09-29T07_48_56_942Z-debug.log
Scripts inside my package.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"buildwebpack": "webpack",
"dev": "webpack-dev-server"
},
"devDependencies": {
"webpack": "^4.20.2",
"webpack-cli": "^3.1.1",
"webpack-dev-server": "^3.1.9"
}
Project Working Directory
https://i.stack.imgur.com/z1v0p.png
I saw that you are using windows system.So maybe you need configure environment variables for this "webpack/lib/node/NodeTemplatePlugin" module. Then it can find the module.
Webpack is inbuilt in React modules when we create a project via create-react-app. That's how Facebook has developed to reduce concentrating about environment setup and look into building the application. So, there's an easy way to create our project avoiding Webpack initiation.
Refer:
https://www.techiediaries.com/react-tutorial/

How to deploy a reactJS application to Heroku

I am trying to deploy a reactJS application to heroku. I am developing on my local machine. I have the following in my package.json file:
"name": "sample-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --open"
}
When testing on my local machine, I type in "npm start" from a command prompt and my browser opens to localhost:8080 and my application starts up.
When I deployed the application to the heroku service and tried to start the application, I get an application error. I then looked at the logs and I see this:
2018-09-06T13:37:57.697321+00:00 app[api]: Release v1 created by user jonathn6#icloud.com
2018-09-06T13:37:57.785389+00:00 app[api]: Enable Logplex by user jonathn6#icloud.com
2018-09-06T13:37:57.785389+00:00 app[api]: Release v2 created by user jonathn6#icloud.com
2018-09-06T13:38:40.000000+00:00 app[api]: Build started by user jonathn6#icloud.com
2018-09-06T13:37:57.697321+00:00 app[api]: Initial release by user jonathn6#icloud.com
2018-09-06T13:39:05.206635+00:00 app[api]: Scaled to web#1:Free by user jonathn6#icloud.com
2018-09-06T13:39:05.188008+00:00 app[api]: Release v3 created by user jonathn6#icloud.com
2018-09-06T13:39:05.188008+00:00 app[api]: Deploy 257b0e8b by user jonathn6#icloud.com
2018-09-06T13:39:08.000000+00:00 app[api]: Build succeeded
2018-09-06T13:39:10.243037+00:00 heroku[web.1]: Starting process with command `npm start`
2018-09-06T13:39:12.459230+00:00 app[web.1]:
2018-09-06T13:39:12.459251+00:00 app[web.1]: > sample-app#1.0.0 start /app
2018-09-06T13:39:12.459255+00:00 app[web.1]:
2018-09-06T13:39:12.459253+00:00 app[web.1]: > webpack-dev-server --open
2018-09-06T13:39:13.990393+00:00 app[web.1]: Project is running at http://localhost:8080/
2018-09-06T13:39:13.990876+00:00 app[web.1]: webpack output is served from /
2018-09-06T13:39:13.990913+00:00 app[web.1]: Content not from webpack is served from /app/public
2018-09-06T13:39:13.990975+00:00 app[web.1]: 404s will fallback to /index.html
2018-09-06T13:39:13.999552+00:00 app[web.1]: internal/child_process.js:323
2018-09-06T13:39:13.999556+00:00 app[web.1]: throw errnoException(err, 'spawn');
2018-09-06T13:39:13.999557+00:00 app[web.1]: ^
2018-09-06T13:39:13.999559+00:00 app[web.1]:
2018-09-06T13:39:13.999560+00:00 app[web.1]: Error: spawn EACCES
2018-09-06T13:39:13.999562+00:00 app[web.1]: at _errnoException (util.js:992:11)
2018-09-06T13:39:13.999564+00:00 app[web.1]: at ChildProcess.spawn (internal/child_process.js:323:11)
2018-09-06T13:39:13.999565+00:00 app[web.1]: at Object.exports.spawn (child_process.js:502:9)
2018-09-06T13:39:13.999567+00:00 app[web.1]: at module.exports (/app/node_modules/opn/index.js:76:26)
2018-09-06T13:39:13.999569+00:00 app[web.1]: at reportReadiness (/app/node_modules/webpack-dev-server/bin/webpack-dev-server.js:474:5)
2018-09-06T13:39:13.999571+00:00 app[web.1]: at Server.server.listen (/app/node_modules/webpack-dev-server/bin/webpack-dev-server.js:439:7)
2018-09-06T13:39:13.999572+00:00 app[web.1]: at Server.returnValue.listeningApp.listen (/app/node_modules/webpack-dev-server/lib/Server.js:615:10)
2018-09-06T13:39:13.999575+00:00 app[web.1]: at emitNone (events.js:106:13)
2018-09-06T13:39:13.999573+00:00 app[web.1]: at Object.onceWrapper (events.js:313:30)
2018-09-06T13:39:13.999576+00:00 app[web.1]: at Server.emit (events.js:208:7)
2018-09-06T13:39:14.008212+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-09-06T13:39:14.008589+00:00 app[web.1]: npm ERR! errno 1
2018-09-06T13:39:14.009730+00:00 app[web.1]: npm ERR! sample-app#1.0.0 start: `webpack-dev-server --open`
2018-09-06T13:39:14.009893+00:00 app[web.1]: npm ERR! Exit status 1
2018-09-06T13:39:14.010124+00:00 app[web.1]: npm ERR!
2018-09-06T13:39:14.010297+00:00 app[web.1]: npm ERR! Failed at the sample-app#1.0.0 start script.
2018-09-06T13:39:14.010464+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2018-09-06T13:39:14.050770+00:00 app[web.1]:
2018-09-06T13:39:14.051004+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2018-09-06T13:39:14.051156+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2018-09-06T13_39_14_011Z-debug.log
2018-09-06T13:39:14.139768+00:00 heroku[web.1]: State changed from starting to crashed
I'm pretty sure I need to change the "start" script to something else but I dont know what I need to change it to. I think.
Any ideas?
You really shouldn't be using dev-server to try and run your app in production. I would roll a simple Express server and use that to deploy your app. You can get this set up very quickly:
npm i -s express
Then create your server.js file in the top level of your app's structure.
Put this code into the server.js file:
const express = require('express')
const app = express()
const path = require('path')
const port = process.env.PORT || 3001
app.use('/', express.static(path.join(__dirname, 'public')))
app.listen(port, () => console.log("Listening on Port", port))
Essentially this just sets up your server and then shows it where to find the index.html page that you're using to load your react app in order to send it to the browser. If your webpack setup outputs the index.html file to a folder other than 'public', just put the name of that folder in where I've put 'public' after '__dirname'.
Now you just need to build your app.
If you're using webpack it's as simple as setting up a "build" script in your package.json:
"build": "webpack -p"
Then make sure Heroku knows how to use your build with another script:
"heroku-postbuild": "npm run build"
Finally set your "start" script to run your server (Heroku uses the name start by default for this):
"start": node server.js
So your package.json scripts should look like this:
"scripts": {
"dev-server": "webpack-dev-server --open"
"start": "node server.js",
"build": "webpack -p",
"heroku-postbuild": "npm run build"
}
Now if you push to Heroku it should run without any issues.
I googled your issue Error: spawn EACCES and found that others have reported something similar when working with Heroku.
Try adding a postinstall to your scripts to see if that helps.
"name": "sample-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --open",
"postinstall": "chmod -R +x node_modules/next/dist/bin/next-* && npm run build",
}
If that doesn't work, you can check out the original thread here to see if any other solution works for you.

Resources