2016-02-27 41 views
0

嘗試使用nodenode --harmony但我繼續得到'SyntaxError:Unexpected token import'錯誤。節點5.6不理解ES6?

我有兩個文件,A.tsB.ts。這裏是他們看什麼喜歡 -

B.ts

export class B { 

} 

A.ts

import {B} from './B'; 

console.log(new B()); 

導致在 -

(function (exports, require, module, __filename, __dirname) { import {B} from './B'; 
                   ^^^^^^ 
SyntaxError: Unexpected token import 
    at exports.runInThisContext (vm.js:53:16) 
    at Module._compile (module.js:387:25) 
    at Object.Module._extensions..js (module.js:422:10) 
    at Module.load (module.js:357:32) 
    at Function.Module._load (module.js:314:12) 
    at Function.Module.runMain (module.js:447:10) 
    at startup (node.js:140:18) 
    at node.js:1001:3 

爲什麼?

+1

你的問題是打字稿,沒有節點,我想。 – Pointy

+0

以上是不是純粹的es6? –

+1

這是ES6。這裏根本沒有TypeScript。 –

回答

0

模塊還在進行中,我不知道,如果試圖拋出的標誌將解決您的錯誤,但這裏的命令:

$ node --v8-options | grep 'in progress' 
--harmony_modules (enable "harmony modules" (in progress)) 
+0

感謝您抽出時間離開您的音樂來回答我的問題。沒有工作。你在用什麼?一個翻譯? –

+0

@MicahWilliamson你很受歡迎,不確定你在問什麼。我使用了一個轉譯器來做什麼?你是否試圖編譯TypeScript(假設是因爲你的.ts擴展名)? – Datsik

+0

我試圖再次運行節點的src文件,而不是構建文件(只是儘量避免額外的複雜性,如果可能的話)。我現在可以針對ES5構建運行。 –

-1

嘗試https://www.npmjs.com/package/ts-node。它允許編譯打字稿節點。

還要確保您的tsconfig.json設置爲使用commonjs進行編譯。 https://github.com/Microsoft/TypeScript/wiki/tsconfig.json

這裏是我的tsconfig.json是什麼樣子:

{ 
    "version": "1.0", 
    "compilerOptions": { 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "target": "es5", 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "removeComments": true, 
    "sourceMap": false 
    } 
}