這裏是我剛剛修改,以測試transpiling過程中使用ES6進口非常基本的快件樣板代碼app.js:在.babelrc無法運行巴貝爾transpiled文件與節點
import "babel-polyfill";
import * as express from "express"
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function() {
console.log('Examdple app listening on port 3000!');
});
{
"presets": [
"es2015"
],
"plugins": ["transform-runtime",
"transform-es2015-classes"]
}
所得transpiled程式-compiled.js:
"use strict";
require("babel-polyfill");
var _express = require("express");
var express = _interopRequireWildcard(_express);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function() {
console.log('Examdple app listening on port 3000!');
});
//# sourceMappingURL=app-compiled.js.map
現在試圖運行/ usr/local/bin目錄/節點應用共mpiled.js:
var app = express();
^
TypeError: express is not a function
或/ usr/local/bin目錄/節點app.js
/Users/Documents/Apps_And_Sites/Js_Apps/projectname/app.js:1
(function (exports, require, module, __filename, __dirname) { import "babel-polyfill";
^^^^^^
SyntaxError: Unexpected reserved word
此外,當我不使用它相同的 '進口' 的錯誤的填充工具,而不封裝功能。
如果這有助於在的package.json依賴關係:
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-core": "^6.9.1",
"babel-eslint": "^6.0.4",
"babel-plugin-syntax-flow": "^6.8.0",
"babel-plugin-transform-class-properties": "^6.9.1",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"eslint-plugin-flowtype": "^2.2.7",
"flow-bin": "^0.26.0"
},
"dependencies": {
"babel-runtime": "^6.9.2",
"express": "^4.13.4"
}
}
在我webstorm IDE,我有通天用這些參數設置:
計劃:node_modules /巴貝爾,CLI /斌/ babel.js
參數:--source-地圖--out文件 $ FileNameWithoutExtension $ -compiled.js $文件路徑$
輸出路徑刷新: $ FileNameWithoutExtension $ -compiled.js:$ FileNameWithoutExtension $ -compiled.js.map
爲什麼不能節點運行巴貝爾transpiled代碼?
謝謝,我應該怎麼導入填充工具,因爲他們寫的不是將其分配給一個變量,採用進口「巴別塔-polyfill「;這會導致錯誤? –
'import'babel-polyfill';'與'require('babel-polyfill')'一樣;''''這是你應該如何使用它的。 –