UNPKG

9.98 kBMarkdownView Raw
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
9Use this to load modules whose location is specified in the `paths` section of `tsconfig.json` or `jsconfig.json`. Both loading at run-time and via API are supported.
10
11Typescript 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
13If you require this package's `tsconfig-paths/register` module it will read the `paths` from `tsconfig.json` or `jsconfig.json` and convert node's module loading calls into to physical file paths that node can load.
14
15## How to install
16
17```
18yarn add --dev tsconfig-paths
19```
20
21or
22
23```
24npm 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
33If `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
41If `process.env.TS_NODE_PROJECT` is set it will be used to resolved tsconfig.json
42
43### With webpack
44
45For webpack please use the [tsconfig-paths-webpack-plugin](https://github.com/dividab/tsconfig-paths-webpack-plugin).
46
47### With mocha and ts-node
48
49As 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
52mocha -r ts-node/register -r tsconfig-paths/register "test/**/*.ts"
53```
54
55### With other commands
56
57As 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
61The 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
92If 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
94For example, create a wrapper script called `tsconfig-paths-bootstrap.js` with the contents below:
95
96```javascript
97const tsConfig = require("./tsconfig.json");
98const tsConfigPaths = require("tsconfig-paths");
99
100const baseUrl = "./"; // Either absolute or relative path. If relative it's resolved to current working directory.
101const cleanup = tsConfigPaths.register({
102 baseUrl,
103 paths: tsConfig.compilerOptions.paths,
104});
105
106// When path registration is no longer needed
107cleanup();
108```
109
110Then run with:
111
112`node -r ./tsconfig-paths-bootstrap.js main.js`
113
114## Configuration Options
115
116You can set options by passing them before the script path, via programmatic usage or via environment variables.
117
118```bash
119ts-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
1301. Use explicit params passed to register
1312. Use `process.env.TS_NODE_PROJECT` to resolve tsConfig.json and the specified baseUrl and paths.
1323. Resolves tsconfig.json from current working directory and the specified baseUrl and paths.
133
134## Programmatic use
135
136The 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
146export interface ExplicitParams {
147 baseUrl: string;
148 paths: { [key: string]: Array<string> };
149 mainFields?: (string | string[])[];
150 addMatchAll?: boolean;
151 cwd?: string;
152}
153
154/**
155 * Installs a custom module load function that can adhere to paths in tsconfig.
156 */
157export function register(explicitParams: ExplicitParams): () => void;
158```
159
160This function will patch the node's module loading so it will look for modules in paths specified by `tsconfig.json` or `jsconfig.json`.
161A function is returned for you to reinstate Node's original module loading.
162
163### loadConfig
164
165```typescript
166export function loadConfig(cwd: string = process.cwd()): ConfigLoaderResult;
167
168export type ConfigLoaderResult =
169 | ConfigLoaderSuccessResult
170 | ConfigLoaderFailResult;
171
172export interface ConfigLoaderSuccessResult {
173 resultType: "success";
174 absoluteBaseUrl: string;
175 paths: { [key: string]: Array<string> };
176}
177
178export interface ConfigLoaderFailResult {
179 resultType: "failed";
180 message: string;
181}
182```
183
184This function loads the `tsconfig.json` or `jsconfig.json`. It will start searching from the specified `cwd` directory. Passing the `tsconfig.json` or `jsconfig.json` file directly instead of a directory also works.
185
186### createMatchPath
187
188```typescript
189/**
190 * Function that can match a path
191 */
192export interface MatchPath {
193 (
194 requestedModule: string,
195 readJson?: Filesystem.ReadJsonSync,
196 fileExists?: (name: string) => boolean,
197 extensions?: ReadonlyArray<string>
198 ): string | undefined;
199}
200
201/**
202 * Creates a function that can resolve paths according to tsconfig paths property.
203 * @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig.
204 * @param paths The paths as specified in tsconfig.
205 * @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names.
206 * @param addMatchAll Add a match-all "*" rule if none is present
207 * @returns a function that can resolve paths.
208 */
209export function createMatchPath(
210 absoluteBaseUrl: string,
211 paths: { [key: string]: Array<string> },
212 mainFields: (string | string[])[] = ["main"],
213 addMatchAll: boolean = true
214): MatchPath {
215```
216
217The `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.
218
219### matchFromAbsolutePaths
220
221```typescript
222/**
223 * Finds a path from tsconfig that matches a module load request.
224 * @param absolutePathMappings The paths to try as specified in tsconfig but resolved to absolute form.
225 * @param requestedModule The required module name.
226 * @param readJson Function that can read json from a path (useful for testing).
227 * @param fileExists Function that checks for existence of a file at a path (useful for testing).
228 * @param extensions File extensions to probe for (useful for testing).
229 * @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names.
230 * @returns the found path, or undefined if no path was found.
231 */
232export function matchFromAbsolutePaths(
233 absolutePathMappings: ReadonlyArray<MappingEntry.MappingEntry>,
234 requestedModule: string,
235 readJson: Filesystem.ReadJsonSync = Filesystem.readJsonFromDiskSync,
236 fileExists: Filesystem.FileExistsSync = Filesystem.fileExistsSync,
237 extensions: Array<string> = Object.keys(require.extensions),
238 mainFields: (string | string[])[] = ["main"]
239): string | undefined {
240```
241
242This function is lower level and requires that the paths as already been resolved to absolute form and sorted in correct order into an array.
243
244### createMatchPathAsync
245
246This is the async version of `createMatchPath`. It has the same signature but with a callback parameter for the result.
247
248### matchFromAbsolutePathsAsync
249
250This is the async version of `matchFromAbsolutePaths`. It has the same signature but with a callback parameter for the result.
251
252## How to publish
253
254```
255yarn version --patch
256yarn version --minor
257yarn version --major
258```
259
260[version-image]: https://img.shields.io/npm/v/tsconfig-paths.svg?style=flat
261[version-url]: https://www.npmjs.com/package/tsconfig-paths
262[build-image]: https://github.com/dividab/tsconfig-paths/workflows/CI/badge.svg
263[build-url]: https://github.com/dividab/tsconfig-paths/actions/workflows/ci.yml?query=branch%3Amaster
264[codecov-image]: https://codecov.io/gh/dividab/tsconfig-paths/branch/master/graph/badge.svg
265[codecov-url]: https://codecov.io/gh/dividab/tsconfig-paths
266[license-image]: https://img.shields.io/github/license/dividab/tsconfig-paths.svg?style=flat
267[license-url]: https://opensource.org/licenses/MIT
268[prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg
269[prettier-url]: https://github.com/prettier/prettier