简体   繁体   English

“类型错误: <class> 不是构造函数”,将其导出到npm包的index.js中

[英]“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.

相关问题 使用babel转译的浏览器的npm软件包的index.js - index.js for npm package for browser transpiled with babel 从 index.js 导出变量并将其导入另一个 class - react.js - exporting a variable from index.js and importing it into another class - react.js vuex-typex` npm软件包的`dist / index.js`的编译以及与Jest的兼容性 - Compilation of `vuex-typex` npm package's `dist/index.js` and compatibility with Jest 未捕获的TypeError:r不是index.js:1的函数 - Uncaught TypeError: r is not a function at index.js:1 这将如何工作...... npm 与 index.js 和 index.html? - How this will work... npm with index.js and index.html? 无法将类导入 index.js 文件 - Cannot import class into index.js file index.js:8 未捕获类型错误:无法读取 index.js:8 处未定义的属性“addEventListener”, - index.js:8 Uncaught TypeError: Cannot read property 'addEventListener' of undefined at index.js:8, index.js:33 未捕获的类型错误:无法在 loadQuiz 中读取未定义的属性“问题”(index.js:33) - index.js:33 Uncaught TypeError: Cannot read property 'question' of undefined at loadQuiz (index.js:33) npm 仅在 index.js 中进行更改时更新 - npm is only updating when changes are made in index.js npm start在浏览器中运行列表目录,而不是index.js - npm start runs listing directory in browser, not index.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM