[英]Can't run babel transpiled files with node
這是我剛剛修改為使用es6導入以測試轉換過程app.js的非常基本的快速樣板代碼:
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!');
});
在.babelrc
{
"presets": [
"es2015"
],
"plugins": ["transform-runtime",
"transform-es2015-classes"]
}
結果轉換代碼app-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 / node app-compiled.js:
var app = express();
^
TypeError: express is not a function
或/ usr / local / bin / node 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
另外,當我不使用polyfill時,如果沒有封裝功能,則會出現相同的'import'錯誤。
如果這有幫助,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中,我使用這些參數設置了babel:
程序:node_modules / babel-cli / bin / babel.js
參數: - source-maps --out-file $ FileNameWithoutExtension $ -compiled.js $ FilePath $
要刷新的輸出路徑:$ FileNameWithoutExtension $ -compiled.js:$ FileNameWithoutExtension $ -compiled.js.map
為什么節點不能運行babel轉換代碼?
您的import
在語法上是正確的,但對您的使用有誤,它應該是:
import express from 'express';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.