Search code examples
angulartypescriptnpm

How to publish TypeScript modules on NPM without "dist" in import?


I'm trying to publish a typescript library on NPM but I can't to have a "good" path after publishing.

I follow several guide on this point but I'm not found a solution.

The problem: So, consider the following structure:

dist
 |- Alls files generated after tsc build
lib
 |- All TS files of library (source)
CHANGELOG.md
LICENSE
package.json
README.md
tsconfig.json

After publishing, for instance in an Angular application, I shall type:

import {Component} from '<library name>/dist/Component';

My question: How can I configure project import to have from '<library name>/Component' instead from '<library name>/dist/Component' please?

Thank you


Solution

  • On suggestions of @joe-clay, I found a solution.

    My new structure is the following:

    dist
     |- Alls files generated after tsc build
     |- package.json
     |- LICENSE
     |- README.md
    src
     |- All TS files of library (source)
    CHANGELOG.md
    LICENSE
    README.md
    tsconfig.json
    

    The dist directory is published on NPM with README.md and LICENSE file for NPM package page.

    The root directory is the entry point on Github with README.md, LICENSE and CHANGELOG.md for development process on Github.

    tsconfig.json is placed on the root because I don't find a solution to have correct build if located inside dist directory.

    In package.json, I add the script "build": "cd ../ && tsc" in order to be able to execute npm run build inside dist directory.

    With this structure, library development and NPM publishing works fine.

    And I can use this import from my application:

    import {Component} from '<library name>/Component';
    

    Thank you again.