2017-09-28 77 views
0

我正嘗試創建一個純ES6應用程序,用於以node/express作爲Web服務器的學習目的。我使用模塊導入,它可以正常工作,因爲你可以在server.js文件中看到,但是當我在index.html中使用這些腳本時,他沒有獲取轉譯文件並調用'Uncaught SyntaxError':我的問題是,如何我可以從index.html訪問轉儲的文件嗎?index.html不加載來自babel的transpiled javascript

server.js文件:

import express from 'express' 

const port = 3000 
const app = express() 

app.set("view options", {layout: false}); 

app.use(express.static(__dirname + '/src')); 

app.get('/', function(req, res) { 
    res.render('index.html'); 
}); 

app.listen(port,()=>{ 
    console.log("server running in port "+ port) 
}) 

的package.json文件:

{ 
    "name": "ntn", 
    ... 
    "scripts": { 
    "start": "nodemon server.js --exec babel-node" 
    }, 
    "dependencies": { 
    "express": "^4.15.4", 
    "nodemon": "^1.12.1" 
    }, 
    "devDependencies": { 
    "babel-cli": "^6.26.0", 
    "babel-core": "^6.26.0", 
    "babel-preset-env": "^1.6.0", 
    "babel-register": "^6.26.0", 
    "jasmine": "^2.8.0" 
    } 
} 

index.html文件:

<!DOCTYPE html> 
<html lang="en"> 
<head> ... </head> 
<body> 
    <h1>Hello Word!</h1> 
    <script src="pages/member/MemberController.js"></script> 
</body> 
</html> 

MemberController.js文件:

import { MemberView } from 'MemberView' 

export class MemberController{ 

    constructor(){ 

    } 
} 

回答

0

您是否正在使用帶有babel的webpack將ES6代碼轉換爲ES5代碼?一旦你安裝了webpack,就有一個叫做「babel-loader」的加載器,它爲你做了轉換。