我有以下es6类,它位于mqttModule.js。
/*jshint esversion: 6*/
import mqtt from 'mqtt';
let client;
class MqttWrapper {
constructor(mqttOptions) {
this.clientId = mqttOptions.clientId;
this.keepAlive = mqttOptions.keepAlive;
this.clean = mqttOptions.clean;
this.reconnectPeriod = mqttOptions.reconnectPeriod;
this.will = mqttOptions.lastWillMessage;
let options = {
clientId: this.clientId,
keepAlive: this.keepAlive,
clean: this.clean,
reconnectPeriod: this.reconnectPeriod,
will: this.will
};
client = mqtt.connect(url, options);
}
}
export {MqttWrapper};
我正在调用它,如下所示。
import MqttWrapper from './mqttModule'
let willMessage = {
topic : 'WillMessage',
payload : 'This is the last will message',
qos : 2,
retain : 'true'
}
let mqttOptions = {
clientId : '101',
keepAlive : 10,
clean : 'false',
reconnectPeriod : '1000',
lastWillMessage : willMessage
}
let mqttWrapperObj = new MqttWrapper(mqttOptions);
当我运行gulp命令时,它会正常运行而没有任何错误。但是当我运行它时,我收到的错误是类型错误。以下是完整的跟踪。
G:\Projects\Kube2\edge-node-sdk-js-test\src\app.js:25
var mqttWrapperObj = new _mqttModule2.default(mqttOptions);
^
TypeError: _mqttModule2.default is not a function
at Object.<anonymous> (app.js:19:22)
at Module._compile (module.js:409:26)
at loader (G:\Projects\Kube2\edge-node-sdk-js-test\node_modules\babel-cli\no
de_modules\babel-register\lib\node.js:158:5)
at Object.require.extensions.(anonymous function) [as .js] (G:\Projects\Kube
2\edge-node-sdk-js-test\node_modules\babel-cli\node_modules\babel-register\lib\n
ode.js:168:7)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at G:\Projects\Kube2\edge-node-sdk-js-test\node_modules\babel-cli\lib\_babel
-node.js:171:48
at Object.<anonymous> (G:\Projects\Kube2\edge-node-sdk-js-test\node_modules\
babel-cli\lib\_babel-node.js:172:7)
at Module._compile (module.js:409:26)
npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\
node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v4.4.4
npm ERR! npm v2.15.1
npm ERR! code ELIFECYCLE
npm ERR! edge-node-sdk-js-test@1.0.0 start: `babel-node --presets es2015 ./src/a
pp.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the edge-node-sdk-js-test@1.0.0 start script 'babel-node --pr
esets es2015 ./src/app.js'.
npm ERR! This is most likely a problem with the edge-node-sdk-js-test package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! babel-node --presets es2015 ./src/app.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs edge-node-sdk-js-test
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls edge-node-sdk-js-test
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! G:\Projects\Kube2\edge-node-sdk-js-test\npm-debug.log
以下是我的gulp文件。
/*jshint esversion: 6 */
const gulp = require('gulp');
const babel = require('gulp-babel');
gulp.task('es6',() => {
return gulp.src('src/app.js')
.pipe(babel({
presets: ['es2015']
}))
.pipe(gulp.dest('build'));
});
gulp.task('default',['es6'],() => {
gulp.watch('src/app.js',['es6'])
});
以下是我运行gulp命令时创建的已编译的app.js文件。
'use strict';
var _mqttModule = require('./mqttModule');
var _mqttModule2 = _interopRequireDefault(_mqttModule);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var willMessage = {
topic: 'WillMessage',
payload: 'This is the last will message',
qos: 2,
retain: 'true'
}; /*jshint esversion: 6*/
var mqttOptions = {
clientId: '101',
keepAlive: 10,
clean: 'false',
reconnectPeriod: '1000',
lastWillMessage: willMessage
};
var mqttWrapperObj = new _mqttModule2.default(mqttOptions);
我是ES6和babel的新手。请指教。