Publish Typescript classes as npm package

I wrote a set of Typescript classes that I publish as an NPM module for our internal package server. My other projects can get this dependency using NPM.

I would like to publish the classes in such a way that they can be imported by referencing the package itself , similar to some other packages I use like Angular.

import { SomeClass } from 'mylibrary'

      

This is my current setup:

I wrote a file ./mylibrary.d.ts

:

export * from './dist/foldera/folderb'
//etc

      

./package.json

:

"name": "mylibrary",
"main": "mylibrary.d.ts",
"files": [
  "mylibrary.d.ts",
  "dist/"
],
"types": "mylibrary.d.ts",
//ommitted other settings

      

src/tsconfig.json

:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "../dist/",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": true,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

      

When I try to import a package using the syntax I want, MS VS Code says everything is fine, but starting my project results in a rather cryptic error message:

Module build failed: Error: Debug Failure. False expression: Output generation failed

      

I feel lost in the jargon of modules, namespaces and packages. Any help is appreciated.

+3


source to share


1 answer


If you put the index.ts file in the root folder of your package (same level as package.json) so that it re-exported all the functions from your src / folder than you can publish it without generating js, meaning that it will only be used in TS projects.

The negative side of this approach is that your package will be compiled with your project every time, and possible changes in the TS environment may make the whole project impossible to compile (I am having problems compiling one of my packages on versions <, 2.4.2 , eg).



// index.ts
export { Class1 } from 'src/Class1'
export { Class2 } from 'src/Class2'

      

+2


source







All Articles