Heroku + react + next.js + wordpress - 语法错误,但我该怎么办?

时间:2017-08-03 00:10:41

标签: node.js reactjs heroku next.js

我使用react + next.js + wordpress api(wp托管在其他地方)。 _document.js是项目全部启动的地方。所以,在我的procfile中,我有:

web: node pages/_document.js

但看起来Heroku不喜欢这样强调。有人可以帮忙吗?

Heroku Logs

...

 2017-08-02T23:25:58.694324+00:00 heroku[web.1]: Starting process with command `node pages/_document.js`
 2017-08-02T23:26:00.609211+00:00 app[web.1]: /app/pages/_document.js:1
 2017-08-02T23:26:00.609234+00:00 app[web.1]: (function (exports, require, module, __filename, __dirname) { import Document, { Head, Main, NextScript } from 'next/document';
 2017-08-02T23:26:00.609235+00:00 app[web.1]:                                                               ^^^^^^
 2017-08-02T23:26:00.609236+00:00 app[web.1]: 
 2017-08-02T23:26:00.609237+00:00 app[web.1]: SyntaxError: Unexpected token import
 2017-08-02T23:26:00.609238+00:00 app[web.1]:     at createScript (vm.js:56:10)
 2017-08-02T23:26:00.609238+00:00 app[web.1]:     at Object.runInThisContext (vm.js:97:10)
 2017-08-02T23:26:00.609239+00:00 app[web.1]:     at Module._compile (module.js:542:28)
 2017-08-02T23:26:00.609240+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:579:10)
 2017-08-02T23:26:00.609240+00:00 app[web.1]:     at Module.load (module.js:487:32)
 2017-08-02T23:26:00.609241+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
 2017-08-02T23:26:00.609242+00:00 app[web.1]:     at Module.runMain (module.js:604:10)
 2017-08-02T23:26:00.609241+00:00 app[web.1]:     at Function.Module._load (module.js:438:3)
 2017-08-02T23:26:00.609243+00:00 app[web.1]:     at run (bootstrap_node.js:389:7)
 2017-08-02T23:26:00.609243+00:00 app[web.1]:     at startup (bootstrap_node.js:149:9)
 2017-08-02T23:26:00.670350+00:00 heroku[web.1]: State changed from starting to crashed
 2017-08-02T23:26:00.673110+00:00 heroku[web.1]: State changed from crashed to starting
 2017-08-02T23:26:00.655633+00:00 heroku[web.1]: Process exited with status 1
 2017-08-02T23:26:04.035702+00:00 heroku[web.1]: Starting process with command `node pages/_document.js`
 2017-08-02T23:26:06.079168+00:00 heroku[web.1]: Process exited with status 1
 2017-08-02T23:26:05.997361+00:00 app[web.1]:                                                               ^^^^^^
....

的package.json

{
  "name": "my-project",
  "author": "me",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "babel-plugin-styled-components": "^1.1.7",
    "babel-preset-stage-0": "^6.24.1",
    "classnames": "^2.2.5",
    "express": "4.14.0",
    "isomorphic-fetch": "2.2.1",
    "lru-cache": "4.0.2",
    "next": "2.0.0-beta.16",
    "nprogress": "0.2.0",
    "prop-types": "^15.5.10",
    "react": "^15.4.2",
    "react-dom": "^15.4.2",
    "styled-components": "^2.1.1"
  },
  "scripts": {
    "build": "next build",
    "start": "node server.js",
    "now-start": "NODE_ENV=production node server.js",
    "test": "jest"
  }
}

1 个答案:

答案 0 :(得分:1)

节点仍然不支持ES6 module syntax。请改用CommonJS。为此,请替换所有:

export default XXX; // <-- ES6 export syntax (NO)

import XXX from 'xxx'; // <-- ES6 import syntax (NO)

使用:

module.exports = XXX; // <-- CommonJS export syntax (YES)

const XXX = require('xxx'); // <-- CommonJS import syntax (YES)

希望这有帮助!

修改

更具体地说,你的Heroku日志抱怨这一行:

import Document, { Head, Main, NextScript } from 'next/document';

将其更改为:

const Document = require('next/document');
const {Head, Main, NextScript} = require('next/document');