语法错误:意外的标识符从“ body-parser”导入bodyParser; …在REACT JS应用中

时间:2019-05-26 18:20:32

标签: reactjs

https://originmaster.com/running-create-react-app-and-express-crae-on-heroku-c39a39fe7851

上最初使用CRA-E for Heroku指令构建的Express上创建react应用。

在本地我可以看到此错误,但是我的应用仍然可以正确处理请求。在Heroku上部署它时,我还会看到相同的错误,然后对于任何请求,我都会在Heroku日志中得到一个H10 "App crashed"

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

该应用是根据Heroku的以下CRA-E说明创建的:https://originmaster.com/running-create-react-app-and-express-crae-on-heroku-c39a39fe7851

package.json根文件是

{
  "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"
  }
}

client / package.json是

{
  "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"
}

服务器/ pacakge.json文件为

{
  "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"
  }
}

server / server.js文件是

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')}`)
})

1 个答案:

答案 0 :(得分:1)

如果要在nodejs应用程序中使用class Player(object): def __init__(self, x, y, velocity): self.x = x self.y = y self.width = 32 self.height = 32 self.velocity = velocity self.render() self.tick() def movement(self): for event in pygame.event.get(): if event.type == pygame.KEYDOWN: print("done") if event.key == pygame.K_w: self.y -= self.velocity elif event.key == pygame.K_s: self.y += self.velocity elif event.key == pygame.K_d: self.x += self.velocity elif event.key == pygame.K_a: self.x -= self.velocity elif event.key == pygame.K_ESCAPE: pygame.quit() elif event.type == pygame.QUIT: pygame.quit() def tick(self): self.movement() def render(self): pygame.draw.rect(window.WINDOW, (255, 0, 0), (self.x, self.y, self.width, self.height)) pygame.display.update() 语句,则需要在运行代码之前由babel对其进行编译。您似乎已经安装了babel软件包,但尚未设置import文件。尝试在.babelrc目录中创建一个.babelrc文件,其内容如下:

server