Aks*_*ood 16 javascript node.js ecmascript-6
我有一个用Javascript ES6编写的课程.当我尝试执行nodemon
命令时,我总是看到这个错误TypeError: Class constructor Client cannot be invoked without 'new'
完整错误如下:
/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:45
return (0, _possibleConstructorReturn3.default)(this, (FBClient.__proto__ || (0, _getPrototypeOf2.default)(FBClient)).call(this, props));
^
TypeError: Class constructor Client cannot be invoked without 'new'
at new FBClient (/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:45:127)
at Object.<anonymous> (/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:195:14)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)
at Module.require (module.js:585:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/akshaysood/Blockchain/fabricSDK/dist/routes/users.js:11:20)
Run Code Online (Sandbox Code Playgroud)
我想要做的是,我创建了一个类,然后创建了该类的实例.然后我试图导出该变量.
类结构定义如下:
class FBClient extends FabricClient{
constructor(props){
super(props);
}
<<< FUNCTIONS >>>
}
Run Code Online (Sandbox Code Playgroud)
我如何尝试导出变量 - >
var client = new FBClient();
client.loadFromConfig(config);
export default client = client;
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到完整的代码> https://hastebin.com/kecacenita.js 由Babel生成的代码> https://hastebin.com/fabewecumo.js
Est*_*ask 34
问题是该类扩展了本机ES6类,并使用Babel转换为ES5.透明的类不能扩展本机类,至少没有其他措施.
class TranspiledFoo extends NativeBar {
constructor() {
super();
}
}
Run Code Online (Sandbox Code Playgroud)
结果像
function TranspiledFoo() {
var _this = NativeBar.call(this);
return _this;
}
// prototypically inherit from NativeBar
Run Code Online (Sandbox Code Playgroud)
由于ES6类只应该调用new
,NativeBar.call
导致错误.
任何最新的Node版本都支持ES6类,不应该对它们进行转换.es2015
应该从Babel配置中排除,最好使用env
预设集来node
定位.
Kaj*_*nus 17
我转译的不是 Javascript 而是 Typescript,遇到了同样的问题。
我编辑了 Typescript 编译器配置文件tsconfig.json
,以生成 ES2017 Javascript 代码:
{
"compilerOptions": {
"target": "ES2017",
Run Code Online (Sandbox Code Playgroud)
而不是默认值,ES2015?- 然后一切正常。
(也许这个答案对使用 Typescript 并在搜索相同错误消息时找到这个问题的人有所帮助,就像我一样。)
小智 7
在package.json
你可以使用目标配置与@babel/preset-env
。设置esmodules
为“真”。
下面是我如何在我的文件中使用的示例:
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"esmodules": true
}
}
],
"@babel/preset-react",
"@babel/preset-flow"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
},
Run Code Online (Sandbox Code Playgroud)
对于那些谁使用TS-节点,它可能是可能是您tsconfig.json
无法通过加载TS-节点。
tsconfig.json
: {
"compilerOptions": {
"target": "ES6",
...
}
}
Run Code Online (Sandbox Code Playgroud)
ts-node --script-mode
或使用--project
来指定您的tsconfig.json
. 归档时间: |
|
查看次数: |
19234 次 |
最近记录: |