Reputation: 5940
Create react app on Express originally built using the CRA-E for Heroku instructions here https://originmaster.com/running-create-react-app-and-express-crae-on-heroku-c39a39fe7851
Locally I can see this error, but my app serves requests correctly anyway. When deploy this on Heroku, I also see this same error, and then for any request I get an H10 "App crashed"
in the Heroku logs.
2019-05-26T18:12:00.682298+00:00 app[web.1]: (function (exports, require, module, __filename, __dirname) { import bodyParser from 'body-parser';
2019-05-26T18:12:00.682300+00:00 app[web.1]: ^^^^^^^^^^
2019-05-26T18:12:00.682302+00:00 app[web.1]:
2019-05-26T18:12:00.682304+00:00 app[web.1]: SyntaxError: Unexpected identifier
2019-05-26T18:12:00.682305+00:00 app[web.1]: at new Script (vm.js:84:7)
2019-05-26T18:12:00.682308+00:00 app[web.1]: at createScript (vm.js:264:10)
2019-05-26T18:12:00.682310+00:00 app[web.1]: at Object.runInThisContext (vm.js:312:10)
2019-05-26T18:12:00.682312+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:696:28)
2019-05-26T18:12:00.682313+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
2019-05-26T18:12:00.682318+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:628:32)
2019-05-26T18:12:00.682319+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:568:12)
2019-05-26T18:12:00.682321+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:560:3)
2019-05-26T18:12:00.682323+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
2019-05-26T18:12:00.682324+00:00 app[web.1]: at executeUserCode (internal/bootstrap/node.js:526:15)
2019-05-26T18:12:00.699031+00:00 app[web.1]: error Command failed with exit code 1.
2019-05-26T18:12:00.699246+00:00 app[web.1]: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
2019-05-26T18:12:00.728654+00:00 app[web.1]: error Command failed with exit code 1.
2019-05-26T18:12:00.728814+00:00 app[web.1]: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
2019-05-26T18:12:00.796019+00:00 heroku[web.1]: Process exited with status 1
2019-05-26T18:12:00.814020+00:00 heroku[web.1]: State changed from starting to crashed
the app was created following these CRA-E instructions for Heroku: https://originmaster.com/running-create-react-app-and-express-crae-on-heroku-c39a39fe7851
The root package.json file is
{
"name": "xxxxxxxxxxxxxx-front",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"bootstrap": "^4.2.1",
"concurrently": "^4.1.0"
},
"scripts": {
"build": "concurrently \"cd client && yarn build\" \"cd server && yarn build\"",
"clean": "concurrently \"rimraf node_modules\" \"cd client && rimraf node_modules build\" \"cd server && rimraf node_modules build\"",
"heroku-postbuild": "yarn build",
"install": "(cd client && yarn) && (cd server && yarn)",
"start": "concurrently \"cd client && PORT=3000 yarn start\" \"cd server && PORT=3001 yarn start\"",
"start:prod": "cd server && yarn start:prod"
},
"engines": {
"node": "11.8.0"
}
}
the client/ package.json is
{
"name": "client",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:3001",
"dependencies": {
"boostrap": "^2.0.0",
"bootstrap": "^4.3.1",
"case-sensitive-paths-webpack-plugin": "^2.2.0",
"d3-hierarchy": "^1.1.8",
"d3-selection": "^1.4.0",
"d3-tree": "^1.0.20",
"node-sass": "^4.11.0",
"prop-types": "^15.7.2",
"react": "^16.8.4",
"react-bootstrap-typeahead": "^3.4.1",
"react-dom": "^16.8.4",
"react-scripts": "2.1.8",
"reactstrap": "^6.5.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"main": "index.js",
"license": "SEE LICENSE IN LICENSE.md",
"author": "Jason Fleetwood-Boldt"
}
the server/ pacakge.json file is
{
"name": "server",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"body-parser": "^1.18.3",
"express": "^4.16.4"
},
"devDependencies": {
"babel-register": "^6.26.0",
"nodemon": "^1.18.9"
},
"scripts": {
"start": "nodemon -r babel-register server.js",
"build": "babel . --ignore node_modules,build --out-dir build",
"start:prod": "node build/server.js"
}
}
the server/server.js file is
import bodyParser from 'body-parser'
import express from 'express'
import path from 'path'
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
const router = express.Router()
const staticFiles = express.static(path.join(__dirname, '../../client/build'))
app.use(staticFiles)
app.use(router)
// any routes not picked up by the server api will be handled by the react router
app.use('/*', staticFiles)
app.set('port', (process.env.PORT || 3001))
app.listen(app.get('port'), () => {
console.log(`Listening on ${app.get('port')}`)
})
Upvotes: 1
Views: 1426
Reputation: 12577
If you want to use import
statements in a nodejs app, your code needs to be transpiled by babel before it's run. You seem to have installed the babel packages, but you haven't set up a .babelrc
file. Try creating a .babelrc
file in the server
directory with the following contents:
{
"presets": ["es2015", "stage-0"],
"plugins": ["transform-runtime"]
}
Upvotes: 2