admin管理员组

文章数量:1355072

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.

本文标签: TypeScript paths not working for built npm packageStack Overflow