publish typescript library with paths & tsconfig-paths

382 Views Asked by At

I wanted to put shared common code into a private npm registry.

Using the below commands & tsconfig has been working great, until I've wanted to make one project depend on another through publishing to an npm registry.

I have several folder and a lot of files, so I'm using paths & tsconfig-paths (imports look clean such as import { Repo } from "adapters/api/repository";).

The common projects builds correctly on its own, but once published and used as a dependency, it's getting build errors when I build the project that uses it.

Common project; builds fine: with rm -rf dist && tsc --sourceMap -p ./ && cp tsconfig.json dist/ && cp .env dist/.

The file structure of the package is:

dist
  src
    adapters
      api
        repository.d.ts
        repository.js
     ...
    application
      interfaces
        interfaceA.d.ts
        interfaceA.js
      ...
    ...
    index.d.ts
    index.js
  tsconfig.json

where src/adapters/api/repository.ts is:

import { InterfaceA } from "application/interfaces/interfaceA";
export interface ApiRepository {
    property: InterfaceA;
    ...
}

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "lib": [
      "es2016"
    ],
    "paths": {
      "*": [
        "src/*"
      ],
      "adapters/*": [
        "src/adapters/*"
      ],
      "application/*": [
        "src/application/*"
      ],
    },
    ...
    "declaration": true,
    "strict": true
  },
  "include": [
    "./src/**/*",
    "./test/**/*"
  ],
  "exclude": [
    "node_modules",
    "dist"
  ]
}

In the other project, where the common project is a dependency, I build with the same command

rm -rf dist && tsc --sourceMap -p ./ && cp tsconfig.json dist/ && cp .env dist/

It's imported with import { ProjectCommon } from "@project/common"; (vs code shows the correct path node_modules/@project/common/dist/src/index).

I can see my common project in node_modules/@project/common.

But when I build I get this error: node_modules/@project/common/dist/src/adapters/api/repository.d.ts:1:37 - error TS2307: Cannot find module 'application/interfaces/interfaceA' or its corresponding type declarations. 1 import { InterfaceA } from "application/interfaces/interfaceA";

The file structures are similar in both project and the tsconfig paths are the same, does this have an impact? Should I not use paths in the common project, since it has to be imported as a library?

I tried to change some path to relative paths, but it's not resolving the build errors. The paths are definitely correct in the common project, that's why I'm surprise to see the other project show an error during its own build, when the common project is a dependency...

When running with ts-node -r tsconfig-paths/register --files ./src/index.ts:

` /home/projects/node_modules/@project/common/dist/src/index.d.ts:1 import { Class } from "adapters/api/another.file"; ^^^^^^

SyntaxError: Cannot use import statement outside a module `

Using these commands & tsconfig has been working great, until I've wanted to make one depend on antoher through publishing to an npm registry.

Looking forward to some insights :) Thanks a lot

0

There are 0 best solutions below