| 1 | # tsconfig-paths
|
|---|
| 2 |
|
|---|
| 3 | [![npm version][version-image]][version-url]
|
|---|
| 4 | [![build][build-image]][build-url]
|
|---|
| 5 | [![Coverage Status][codecov-image]][codecov-url]
|
|---|
| 6 | [![MIT license][license-image]][license-url]
|
|---|
| 7 | [![code style: prettier][prettier-image]][prettier-url]
|
|---|
| 8 |
|
|---|
| 9 | Use this to load modules whose location is specified in the `paths` section of `tsconfig.json`. Both loading at run-time and via API are supported.
|
|---|
| 10 |
|
|---|
| 11 | Typescript by default mimics the Node.js runtime resolution strategy of modules. But it also allows the use of [path mapping](https://www.typescriptlang.org/docs/handbook/module-resolution.html) which allows arbitrary module paths (that doesn't start with "/" or ".") to be specified and mapped to physical paths in the filesystem. The typescript compiler can resolve these paths from `tsconfig` so it will compile OK. But if you then try to execute the compiled files with node (or ts-node), it will only look in the `node_modules` folders all the way up to the root of the filesystem and thus will not find the modules specified by `paths` in `tsconfig`.
|
|---|
| 12 |
|
|---|
| 13 | If you require this package's `tsconfig-paths/register` module it will read the `paths` from `tsconfig.json` and convert node's module loading calls into to physical file paths that node can load.
|
|---|
| 14 |
|
|---|
| 15 | ## How to install
|
|---|
| 16 |
|
|---|
| 17 | ```
|
|---|
| 18 | yarn add --dev tsconfig-paths
|
|---|
| 19 | ```
|
|---|
| 20 |
|
|---|
| 21 | or
|
|---|
| 22 |
|
|---|
| 23 | ```
|
|---|
| 24 | npm install --save-dev tsconfig-paths
|
|---|
| 25 | ```
|
|---|
| 26 |
|
|---|
| 27 | ## How to use
|
|---|
| 28 |
|
|---|
| 29 | ### With node
|
|---|
| 30 |
|
|---|
| 31 | `node -r tsconfig-paths/register main.js`
|
|---|
| 32 |
|
|---|
| 33 | If `process.env.TS_NODE_BASEURL` is set it will override the value of `baseUrl` in tsconfig.json:
|
|---|
| 34 |
|
|---|
| 35 | `TS_NODE_BASEURL=./dist node -r tsconfig-paths/register main.js`
|
|---|
| 36 |
|
|---|
| 37 | ### With ts-node
|
|---|
| 38 |
|
|---|
| 39 | `ts-node -r tsconfig-paths/register main.ts`
|
|---|
| 40 |
|
|---|
| 41 | If `process.env.TS_NODE_PROJECT` is set it will be used to resolved tsconfig.json
|
|---|
| 42 |
|
|---|
| 43 | ### With webpack
|
|---|
| 44 |
|
|---|
| 45 | For webpack please use the [tsconfig-paths-webpack-plugin](https://github.com/dividab/tsconfig-paths-webpack-plugin).
|
|---|
| 46 |
|
|---|
| 47 | ### With mocha and ts-node
|
|---|
| 48 |
|
|---|
| 49 | As of Mocha >= 4.0.0 the `--compiler` was [deprecated](https://github.com/mochajs/mocha/wiki/compilers-deprecation). Instead `--require` should be used. You also have to specify a glob that includes `.ts` files because mocha looks after files with `.js` extension by default.
|
|---|
| 50 |
|
|---|
| 51 | ```bash
|
|---|
| 52 | mocha -r ts-node/register -r tsconfig-paths/register "test/**/*.ts"
|
|---|
| 53 | ```
|
|---|
| 54 |
|
|---|
| 55 | ### With other commands
|
|---|
| 56 |
|
|---|
| 57 | As long as the command has something similar to a `--require` option that can load a module before it starts, tsconfig-paths should be able to work with it.
|
|---|
| 58 |
|
|---|
| 59 | ### With `ts-node` and VSCode
|
|---|
| 60 |
|
|---|
| 61 | The following is an example configuration for the `.vscode/launch.json`.
|
|---|
| 62 |
|
|---|
| 63 | ```js
|
|---|
| 64 | {
|
|---|
| 65 | "version": "0.2.0",
|
|---|
| 66 | "configurations": [
|
|---|
| 67 | {
|
|---|
| 68 | "name": "Debug Functions",
|
|---|
| 69 | "request": "launch",
|
|---|
| 70 | "type": "node",
|
|---|
| 71 | "runtimeArgs": [
|
|---|
| 72 | "-r",
|
|---|
| 73 | "${workspaceFolder}/functions/node_modules/ts-node/register",
|
|---|
| 74 | "-r",
|
|---|
| 75 | "${workspaceFolder}/functions/node_modules/tsconfig-paths/register"
|
|---|
| 76 | ],
|
|---|
| 77 | "args": ["${workspaceFolder}/functions/src/index.ts"],
|
|---|
| 78 | "cwd": "${workspaceFolder}",
|
|---|
| 79 | "protocol": "inspector",
|
|---|
| 80 | "env": {
|
|---|
| 81 | "NODE_ENV": "development",
|
|---|
| 82 | "TS_NODE_PROJECT": "${workspaceFolder}/functions/tsconfig.json"
|
|---|
| 83 | },
|
|---|
| 84 | "outFiles": ["${workspaceFolder}/functions/lib/**/*.js"]
|
|---|
| 85 | }
|
|---|
| 86 | ]
|
|---|
| 87 | }
|
|---|
| 88 | ```
|
|---|
| 89 |
|
|---|
| 90 | ## Bootstrapping with explicit params
|
|---|
| 91 |
|
|---|
| 92 | If you want more granular control over tsconfig-paths you can bootstrap it. This can be useful if you for instance have compiled with `tsc` to another directory where `tsconfig.json` doesn't exists.
|
|---|
| 93 |
|
|---|
| 94 | For example, create a wrapper script called `tsconfig-paths-bootstrap.js` with the contents below:
|
|---|
| 95 |
|
|---|
| 96 | ```javascript
|
|---|
| 97 | const tsConfig = require("./tsconfig.json");
|
|---|
| 98 | const tsConfigPaths = require("tsconfig-paths");
|
|---|
| 99 |
|
|---|
| 100 | const baseUrl = "./"; // Either absolute or relative path. If relative it's resolved to current working directory.
|
|---|
| 101 | const cleanup = tsConfigPaths.register({
|
|---|
| 102 | baseUrl,
|
|---|
| 103 | paths: tsConfig.compilerOptions.paths,
|
|---|
| 104 | });
|
|---|
| 105 |
|
|---|
| 106 | // When path registration is no longer needed
|
|---|
| 107 | cleanup();
|
|---|
| 108 | ```
|
|---|
| 109 |
|
|---|
| 110 | Then run with:
|
|---|
| 111 |
|
|---|
| 112 | `node -r ./tsconfig-paths-bootstrap.js main.js`
|
|---|
| 113 |
|
|---|
| 114 | ## Configuration Options
|
|---|
| 115 |
|
|---|
| 116 | You can set options by passing them before the script path, via programmatic usage or via environment variables.
|
|---|
| 117 |
|
|---|
| 118 | ```bash
|
|---|
| 119 | ts-node --project customLocation/tsconfig.json -r tsconfig-paths/register "test/**/*.ts"
|
|---|
| 120 | ```
|
|---|
| 121 |
|
|---|
| 122 | ### CLI and Programmatic Options
|
|---|
| 123 |
|
|---|
| 124 | _Environment variable denoted in parentheses._
|
|---|
| 125 |
|
|---|
| 126 | - `-P, --project [path]` Path to TypeScript JSON project file (`TS_NODE_PROJECT`)
|
|---|
| 127 |
|
|---|
| 128 | ## Config loading process
|
|---|
| 129 |
|
|---|
| 130 | 1. Use explicit params passed to register
|
|---|
| 131 | 2. Use `process.env.TS_NODE_PROJECT` to resolve tsConfig.json and the specified baseUrl and paths.
|
|---|
| 132 | 3. Resolves tsconfig.json from current working directory and the specified baseUrl and paths.
|
|---|
| 133 |
|
|---|
| 134 | ## Programmatic use
|
|---|
| 135 |
|
|---|
| 136 | The public API consists of these functions:
|
|---|
| 137 |
|
|---|
| 138 | - [register](#register)
|
|---|
| 139 | - [loadConfig](#loadConfig)
|
|---|
| 140 | - [createMatchPath](#createMatchPath) / [createMatchPathAsync](#createMatchPathAsync)
|
|---|
| 141 | - [matchFromAbsolutePaths](#matchFromAbsolutePaths) / [matchFromAbsolutePathsAsync](#matchFromAbsolutePathsAsync)
|
|---|
| 142 |
|
|---|
| 143 | ### register
|
|---|
| 144 |
|
|---|
| 145 | ```typescript
|
|---|
| 146 | export interface ExplicitParams {
|
|---|
| 147 | baseUrl: string;
|
|---|
| 148 | paths: { [key: string]: Array<string> };
|
|---|
| 149 | mainFields?: Array<string>;
|
|---|
| 150 | addMatchAll?: boolean;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /**
|
|---|
| 154 | * Installs a custom module load function that can adhere to paths in tsconfig.
|
|---|
| 155 | */
|
|---|
| 156 | export function register(explicitParams: ExplicitParams): () => void;
|
|---|
| 157 | ```
|
|---|
| 158 |
|
|---|
| 159 | This function will patch the node's module loading so it will look for modules in paths specified by tsconfig.json.
|
|---|
| 160 | A function is returned for you to reinstate Node's original module loading.
|
|---|
| 161 |
|
|---|
| 162 | ### loadConfig
|
|---|
| 163 |
|
|---|
| 164 | ```typescript
|
|---|
| 165 | export function loadConfig(cwd: string = process.cwd()): ConfigLoaderResult;
|
|---|
| 166 |
|
|---|
| 167 | export type ConfigLoaderResult =
|
|---|
| 168 | | ConfigLoaderSuccessResult
|
|---|
| 169 | | ConfigLoaderFailResult;
|
|---|
| 170 |
|
|---|
| 171 | export interface ConfigLoaderSuccessResult {
|
|---|
| 172 | resultType: "success";
|
|---|
| 173 | absoluteBaseUrl: string;
|
|---|
| 174 | paths: { [key: string]: Array<string> };
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | export interface ConfigLoaderFailResult {
|
|---|
| 178 | resultType: "failed";
|
|---|
| 179 | message: string;
|
|---|
| 180 | }
|
|---|
| 181 | ```
|
|---|
| 182 |
|
|---|
| 183 | This function loads the tsconfig.json. It will start searching from the specified `cwd` directory. Passing the tsconfig.json file directly instead of a directory also works.
|
|---|
| 184 |
|
|---|
| 185 | ### createMatchPath
|
|---|
| 186 |
|
|---|
| 187 | ```typescript
|
|---|
| 188 | /**
|
|---|
| 189 | * Function that can match a path
|
|---|
| 190 | */
|
|---|
| 191 | export interface MatchPath {
|
|---|
| 192 | (
|
|---|
| 193 | requestedModule: string,
|
|---|
| 194 | readJson?: Filesystem.ReadJsonSync,
|
|---|
| 195 | fileExists?: (name: string) => boolean,
|
|---|
| 196 | extensions?: ReadonlyArray<string>
|
|---|
| 197 | ): string | undefined;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /**
|
|---|
| 201 | * Creates a function that can resolve paths according to tsconfig paths property.
|
|---|
| 202 | * @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig.
|
|---|
| 203 | * @param paths The paths as specified in tsconfig.
|
|---|
| 204 | * @param mainFields A list of package.json field names to try when resolving module files.
|
|---|
| 205 | * @param addMatchAll Add a match-all "*" rule if none is present
|
|---|
| 206 | * @returns a function that can resolve paths.
|
|---|
| 207 | */
|
|---|
| 208 | export function createMatchPath(
|
|---|
| 209 | absoluteBaseUrl: string,
|
|---|
| 210 | paths: { [key: string]: Array<string> },
|
|---|
| 211 | mainFields: string[] = ["main"],
|
|---|
| 212 | addMatchAll: boolean = true
|
|---|
| 213 | ): MatchPath {
|
|---|
| 214 | ```
|
|---|
| 215 |
|
|---|
| 216 | The `createMatchPath` function will create a function that can match paths. It accepts `baseUrl` and `paths` directly as they are specified in tsconfig and will handle resolving paths to absolute form. The created function has the signature specified by the type `MatchPath` above.
|
|---|
| 217 |
|
|---|
| 218 | ### matchFromAbsolutePaths
|
|---|
| 219 |
|
|---|
| 220 | ```typescript
|
|---|
| 221 | /**
|
|---|
| 222 | * Finds a path from tsconfig that matches a module load request.
|
|---|
| 223 | * @param absolutePathMappings The paths to try as specified in tsconfig but resolved to absolute form.
|
|---|
| 224 | * @param requestedModule The required module name.
|
|---|
| 225 | * @param readJson Function that can read json from a path (useful for testing).
|
|---|
| 226 | * @param fileExists Function that checks for existence of a file at a path (useful for testing).
|
|---|
| 227 | * @param extensions File extensions to probe for (useful for testing).
|
|---|
| 228 | * @param mainFields A list of package.json field names to try when resolving module files.
|
|---|
| 229 | * @returns the found path, or undefined if no path was found.
|
|---|
| 230 | */
|
|---|
| 231 | export function matchFromAbsolutePaths(
|
|---|
| 232 | absolutePathMappings: ReadonlyArray<MappingEntry.MappingEntry>,
|
|---|
| 233 | requestedModule: string,
|
|---|
| 234 | readJson: Filesystem.ReadJsonSync = Filesystem.readJsonFromDiskSync,
|
|---|
| 235 | fileExists: Filesystem.FileExistsSync = Filesystem.fileExistsSync,
|
|---|
| 236 | extensions: Array<string> = Object.keys(require.extensions),
|
|---|
| 237 | mainFields: string[] = ["main"]
|
|---|
| 238 | ): string | undefined {
|
|---|
| 239 | ```
|
|---|
| 240 |
|
|---|
| 241 | This function is lower level and requires that the paths as already been resolved to absolute form and sorted in correct order into an array.
|
|---|
| 242 |
|
|---|
| 243 | ### createMatchPathAsync
|
|---|
| 244 |
|
|---|
| 245 | This is the async version of `createMatchPath`. It has the same signature but with a callback parameter for the result.
|
|---|
| 246 |
|
|---|
| 247 | ### matchFromAbsolutePathsAsync
|
|---|
| 248 |
|
|---|
| 249 | This is the async version of `matchFromAbsolutePaths`. It has the same signature but with a callback parameter for the result.
|
|---|
| 250 |
|
|---|
| 251 | ## How to publish
|
|---|
| 252 |
|
|---|
| 253 | ```
|
|---|
| 254 | yarn version --patch
|
|---|
| 255 | yarn version --minor
|
|---|
| 256 | yarn version --major
|
|---|
| 257 | ```
|
|---|
| 258 |
|
|---|
| 259 | [version-image]: https://img.shields.io/npm/v/tsconfig-paths.svg?style=flat
|
|---|
| 260 | [version-url]: https://www.npmjs.com/package/tsconfig-paths
|
|---|
| 261 | [build-image]: https://github.com/dividab/tsconfig-paths/workflows/CI/badge.svg
|
|---|
| 262 | [build-url]: https://github.com/dividab/tsconfig-paths/actions/workflows/ci.yml?query=branch%3Amaster
|
|---|
| 263 | [codecov-image]: https://codecov.io/gh/dividab/tsconfig-paths/branch/master/graph/badge.svg
|
|---|
| 264 | [codecov-url]: https://codecov.io/gh/dividab/tsconfig-paths
|
|---|
| 265 | [license-image]: https://img.shields.io/github/license/dividab/tsconfig-paths.svg?style=flat
|
|---|
| 266 | [license-url]: https://opensource.org/licenses/MIT
|
|---|
| 267 | [prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg
|
|---|
| 268 | [prettier-url]: https://github.com/prettier/prettier
|
|---|