[英]“TypeError: <class> is not a constructor” after exporting it in index.js at npm package
I created the npm package tcx-file-class which handles tcx files the way I want to analyse the data afterwards. 我创建了npm软件包tcx-file-class ,该软件包以我以后要分析数据的方式处理tcx文件。 So it creates (among others) an Activity class.
因此,它(除其他外)创建了一个Activity类。
Activity class exported from the package in the index.ts file in this way: 通过以下方式从包中的index.ts文件中导出的活动类:
import TcxFile from "./dist/classes/tcxFile"
import Activity from "./dist/classes/activity"
//other imports here
export { TcxFile, Activity,
//other classes
}
So when I try to import the class from the package index file like this: 因此,当我尝试从包索引文件中导入类时,如下所示:
import {Activity} from 'tcx-file-class'
const a = new Activity();
I get the following error: 我收到以下错误:
const a = new tcx_file_class_1.Activity();
^
TypeError: tcx_file_class_1.Activity is not a constructor
at Object.<anonymous> (/Users/stratis/Desktop/development/production/ts/events/dist/index.js:11:11)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
at startup (internal/bootstrap/node.js:228:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:575:3)
but when I import the class from the package class file, like this: 但是当我从包类文件中导入类时,如下所示:
import {Activity} from '../node_modules/tcx-file-class/dist/classes/activity'
const a = new Activity();
everything work's just fine. 一切正常。
So, I guess that the exporting procedure is the problem but I can't spot the error. 因此,我猜想导出过程是问题所在,但我无法发现错误。 Any suggestions?
有什么建议么?
It looks to me (and as CertainPerformance pointed out in the comments) that You are exporting Activity as a named export like 在我看来(正如评论中的SomePerformance所指出的那样),您正在将Activity导出为命名的导出,例如
export { Activity }
and not export default Activity
export { Activity }
而不export default Activity
You will need to change your index.ts file's imports to 您将需要将index.ts文件的导入更改为
import {TcxFile} from "./dist/classes/tcxFile"
import {Activity} from "./dist/classes/activity"
to be able to use named imports. 才能使用命名的导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.