【发布时间】:2018-07-25 01:51:54
【问题描述】:
我正在尝试制作一个 NPM 包,在他们运行 npm install <package> 之后,有人可以使用我的类来实例化它。截至目前,它在开发中运行良好,但通过 webpack 捆绑后,我使用 npm 安装包,但它立即被 TypeError: index_1.MyClass is not a constructor 炸毁
my-class.ts
export class MyClass {
constructor() {
}
method1(): void {
console.log('im a method!');
}
}
index.ts
export { MyClass } from './my-class';
main.ts(我使用 npm install 的主要应用程序代码)
import { MyClass } from 'npm-package-directory';
const myClass = new MyClass(); // <-- exception thrown
myClass.method1();
webpack.config.js
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
plugins: [
new CopyWebpackPlugin([
{
from: 'package.json',
to: 'package.json',
toType: 'file'
}
]),
new CopyWebpackPlugin([
{
from: 'README.md',
to: 'README.md',
toType: 'file'
}
])
],
devtool: 'source-map',
entry: './src/my-class.ts',
resolve: {
extensions: [
'.ts'
]
},
output: {
path: __dirname + '/dist',
filename: '[name].js'
},
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.ts?$/, loader: 'ts-loader' }
]
},
target: 'node'
}
我尝试了多种不同风格的导出类,但每次都遇到相同的异常。我尝试了许多类似于this one 的其他 SOF 问题,但没有运气。我去哪儿了?
注意以上是示例代码,旨在简化问题,但很乐意提供实际设置的 github 存储库。
【问题讨论】:
-
它是一个角度应用程序吗?您是否在 app.module.ts 中导入了模块?
-
不,不是 Angular 应用程序。这是直接的 TypeScript/Node。 Angular 会让我需要在编译时将它添加到我的 app.module.ts 中变得更加明显。这一路编译没有任何错误。只是运行时错误。
-
目前我的 webpack.config.js 正在走这条路……
标签: javascript typescript ecmascript-6 es6-modules