最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

TypeScript paths not working for built npm package - Stack Overflow

programmeradmin1浏览0评论

I'm working on writing a client API wrapper for some internal testing. Essentially it's going to act as a npm package our company can use internally to do API testing (using Playwright's API Request library).

The issue I ran into is it would not work any time I used my "built in paths".

For example here is my tsconfig.json:

{
  "compilerOptions": {
    "outDir": "dist",
    "module": "CommonJS",
    "target": "ES6",
    "declaration": true,
    "sourceMap": true,
    "strict": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "baseUrl": ".",
    "esModuleInterop": true,
    "paths": {
      "@fixtures/*": ["lib/fixtures/*"],
      "@constants/*": ["lib/constants/*"],
    }
  },
  "include": ["lib"],
  "exclude": ["node_modules", "dist", "test-results", "tests"]
}

Right now my uppermost root directly looks like this:

lib
-api
--client
---(various client classes.ts)
--baseclient.ts
--index.ts (This is where I am pulling in all my client class files that have API methods)
-constants
-fixtures
...rest of the playwright files

My idea is essentially pulling in all the client class files into that index.ts, then using that as my wrapper. My package.json has main defined as dist/api/index.js.

This does work, however it only works if I switch my imports from something like:

  • this: import someFixture from "@fixtures/cool-fixture/foo-fixture.json"

  • to this: import someFixture from "../../fixtures/cool-fixture/foo-fixture.json"

That goes for all the "paths" I've defined. I'm assuming something is wrong with my tsconfig.json file; these all work fine within Playwright itself, but as soon as TypeScript builds it (using tsc build) it cannot find any file I've set up with the paths.

发布评论

评论列表(0)

  1. 暂无评论