2017-05-08 61 views
1

將SSR添加到我的應用後,我最終在服務器的index.js文件中使用了一些ES6,現在我遇到了讓它運行的問題Heroku再次。意外的保留字:import - Webpack,Babel,Node,React

我已經做了大量的研究,並且明白使用ES6需要babel來傳輸服務器文件,但幾乎所有人的建議我都能找到,通常需要.babelrc文件或指定/安裝預設,但從我所知道的情況來看,我已經做到了。

我發現,我越努力解決此,我變得越糊塗,所以我想在這裏對一些討論:)感謝大家伸出手(全碼:https://github.com/trm313/mern-boiler

Heroku的錯誤

2017-05-08T14:35:04.470656+00:00 app[web.1]: [32m[nodemon] starting `babel-node index.js --presets react,es2015`[39m 
2017-05-08T14:35:04.545063+00:00 app[web.1]: You have mistakenly installed the `babel` package, which is a no-op in Babel 6. 
2017-05-08T14:35:04.545069+00:00 app[web.1]: Babel's CLI commands have been moved from the `babel` package to the `babel-cli` package. 
2017-05-08T14:35:04.545070+00:00 app[web.1]: 
2017-05-08T14:35:04.545071+00:00 app[web.1]:  npm uninstall -g babel 
2017-05-08T14:35:04.545072+00:00 app[web.1]:  npm install --save-dev babel-cli 
2017-05-08T14:35:04.545072+00:00 app[web.1]: 
2017-05-08T14:35:04.545073+00:00 app[web.1]: See http://babeljs.io/docs/usage/cli/ for setup instructions. 
2017-05-08T14:35:04.554946+00:00 app[web.1]: [31m[nodemon] app crashed - waiting for file changes before starting...[39m 

以下腳本,「故宮運行啓動」本地工作正常,但是當上傳到Heroku的失敗。它抱怨說,我需要使用「巴貝爾-CLI」包,而不是「巴別塔」,這我已經在做。(同爲「啓動開發」)

的package.json

{ 
    "name": "react-authentication", 
    "version": "1.0.0", 
    "engines": { 
    "node": "4.4.3", 
    "npm": "4.2.0" 
    }, 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "dev": "nodemon --use-strict index.js", 
    "bundle": "webpack", 
    "start-old": "node index.js", 
    "postinstall": "webpack -p", 
    "start-dev": "babel-node index.js --presets react,es2015", 
    "start": "nodemon index.js --exec babel-node --presets react,es2015", 
    "build": "babel lib -d dist --presets react,es2015", 
    "serve": "node dist/index.js", 
    "prod": "NODE_ENV=production node index.js" 
    }, 
    "keywords": [], 
    "author": "", 
    "license": "ISC", 
    "dependencies": { 
    "axios": "^0.15.3", 
    "babel-cli": "^6.24.1", 
    "babel-core": "^6.23.1", 
    "babel-loader": "^6.3.2", 
    "babel-preset-es2015": "^6.22.0", 
    "babel-preset-react": "^6.23.0", 
    "babel-register": "^6.24.1", 
    "bcryptjs": "^2.4.3", 
    "body-parser": "^1.16.1", 
    "client-sessions": "^0.7.0", 
    "cookie-parser": "^1.4.3", 
    "express": "^4.14.1", 
    "express-session": "^1.15.1", 
    "fs": "0.0.1-security", 
    "jsonwebtoken": "^7.3.0", 
    "material-ui": "^0.17.0", 
    "mongoose": "^4.8.3", 
    "nodemon": "^1.11.0", 
    "passport": "^0.3.2", 
    "passport-facebook": "^2.1.1", 
    "passport-local": "^1.0.0", 
    "path": "^0.12.7", 
    "react": "^15.4.2", 
    "react-dom": "^15.4.2", 
    "react-redux": "^5.0.3", 
    "react-router": "^3.0.2", 
    "react-tap-event-plugin": "^2.0.1", 
    "redux": "^3.6.0", 
    "redux-promise": "^0.5.3", 
    "redux-thunk": "^2.2.0", 
    "validator": "^6.2.1", 
    "webpack": "^2.2.1" 
    }, 
    "devDependencies": { 
    "babel-core": "^6.24.1", 
    "babel-loader": "^6.4.1", 
    "babel-preset-es2015": "^6.24.1", 
    "babel-preset-react": "^6.24.1", 
    "babel-preset-stage-0": "^6.24.1", 
    "nodemon": "^1.11.0", 
    "webpack": "^2.2.1" 
    } 
} 

webpack.config.js

const path = require('path'); 
module.exports = { 
    // the entry file for the bundle 
    entry: path.join(__dirname, '/client/src/app.js'), 

    // the bundle file we will get in the result 
    output: { 
    path: path.join(__dirname, '/client/dist/js'), 
    filename: 'app.js', 
    }, 

    module: { 

    // apply loaders to files that meet given conditions 
    loaders: [{ 
     test: /\.js$/, 
     include: path.join(__dirname, '/client/src'), 
     loader: 'babel-loader', 
     query: { 
     presets: ["react", "es2015"] 
     } 
    }], 
    }, 
    resolve: { 
    extensions: ['.js', '.jsx'] 
    }, 
    // start Webpack in a watch mode, so Webpack will rebuild the bundle on changes 
    watch: false 
}; 

.babelrc

{ 
    "presets": ["react", "es2015"] 
} 

回答

0

嘗試將啓動腳本更改爲「node server.js」而非nodemon。

+0

嗨吉米,謝謝我也試過,但它失敗了同樣的錯誤 – trm313

+0

@ trm313我環顧四周,但無法找到任何東西。你在哪裏看到你張貼圖像的Heroku錯誤?當你嘗試將你的代碼推送到heroku master時,你會得到這些錯誤嗎?嘗試刪除「--exec babel-node --presets react,es2015」並查看它是否有所作爲,否則請訪問https://dashboard.heroku.com/apps/username/activity並查看構建日誌並看看你是否找到任何東西! –

+0

感謝您的環顧四周。我發現如果從開始序列中剝離babel命令(包括預設標誌),我無法讓我的應用程序在本地運行。這是當我得到「意外的保留字:導入」錯誤 但是,我無法讓heroku接受babel命令。當我嘗試我得到有關不使用'babel'的babel錯誤。 Heroku日誌我在看,顯示babel錯誤來自https://dashboard.heroku.com/apps/ /日誌。活動頁面顯示成功構建,但是然後應用程序進入崩潰狀態 – trm313

相關問題