Javascript <;类别>;不是构造函数
我正在尝试制作一个NPM包,其中有人可以在运行Javascript <;类别>;不是构造函数,javascript,typescript,ecmascript-6,es6-modules,Javascript,Typescript,Ecmascript 6,Es6 Modules,我正在尝试制作一个NPM包,其中有人可以在运行NPM install后使用我的类来提示它。到目前为止,它在开发过程中运行良好,但在通过webpack将其捆绑起来之后,我使用npm安装了该包,但它立即以TypeError爆炸:index_1.MyClass不是构造函数 我的班级。ts export class MyClass { constructor() { } method1(): void { console.log('im a method!');
NPM install
后使用我的类来提示它。到目前为止,它在开发过程中运行良好,但在通过webpack将其捆绑起来之后,我使用npm安装了该包,但它立即以TypeError爆炸:index_1.MyClass不是构造函数
我的班级。ts
export class MyClass {
constructor() {
}
method1(): void {
console.log('im a method!');
}
}
export { MyClass } from './my-class';
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'
}
index.ts
export class MyClass {
constructor() {
}
method1(): void {
console.log('im a method!');
}
}
export { MyClass } from './my-class';
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'
}
main.ts(我在其中使用npm安装的主应用程序代码)
我尝试了多种不同风格的导出类,但每次都遇到相同的异常。我试着问了很多其他类似的SOF问题,但没有成功。我在哪下车
注意以上是试图简化问题的示例代码,但很乐意提供实际设置的github repo。假设这是一个typescript项目(通过此问题的标记),这似乎是需要通过在
package.json
和webpack配置文件上正确设置来解决的配置问题
如果您可以共享包.json
中的内容,这会很有帮助,但我认为查看并尝试将挂钩与webpack build cli脚本正确链接将是解决问题的良好起点
我建议您确保:
Webpack构建过程(或您用于typescript编译的任何方法)与钩子链接,钩子在您本地的npm安装
后触发,当有人通过npm安装
已编译的javascript包通过package.json正确公开
至少,您可能需要检查是否需要实际编译typescript源代码,这取决于您是否只支持typescript
这是离题的,但不要使用webpack,除非您确实需要使用webpack。通常,如果您正在编写npm模块,使用typescript编译器本身就足够了。它是角度应用程序吗?您是否已在app.module.ts中导入模块?不,不是角度应用程序。这是直线型脚本/节点。Angular会使我需要在编译时将其添加到app.module.ts中这一点更加明显。这一过程一直编译,没有任何错误。只是一个运行时错误。我正在寻找一条路径,这是我的webpack.config.js当前的一些东西……这是完整的回购协议(非常小)。我想你对这个网页的使用可能是对的。由于这是一个节点模块,webpack可能有点过头了。看起来webpack是个问题,我只需要一个不同的构建过程。我只是在挖掘webpack的惊人之处,并想玩玩它。嗨@mwilson,嗯,我想在你的package.json上添加webpack compile cli命令的安装后钩子,并将webpack compile dependencies移到dependency
(非开发)将解决你的问题。(这可能还包括打字模块
),但现在这将引入(如果您足够关心的话)模块用户需要安装webpack作为依赖项来编译npm模块的问题。如果您只需要编译typescript,只需运行tsc
就足够了(但现在您需要正确设置tsconfig.json),谢谢@kairun。我来试试看我能走多远。