JamesYin
JamesYin

Reputation: 742

Can not import my custom typescript npm package

I make a typescript npm package, simply like this:

Core.ts

class Core{
    constructor(){}
    //some functions
}
export = Core;

Core.d.ts

declare class Core {
   constructor(){}
   //some functions
}
export = Core;

Package.json

{
  "name": "@company/Core",
  "main": "dist/Core.js",
  "typings": "dist/Core" //.d.ts
  //etc
}

I make this module with webpack and ts-loader.

In my project, index.ts

import Core = require('@company/Core');
let core = new Core(); //Error, Core is not a function

I have tried to change export and import in some form but no luck. Do I miss some knowlage?

Upvotes: 1

Views: 353

Answers (1)

JamesYin
JamesYin

Reputation: 742

Finally, I solve my promble by myself. I add two properties in webpack configuration file, then recompile it and works well.

webpack.config.js

module.exports = {
    output: {
        libraryTarget: 'commonjs'
    }
    //etc
}

Upvotes: 1

Related Questions