Gathering detailed insights and metrics for tsconfig-paths
Gathering detailed insights and metrics for tsconfig-paths
Gathering detailed insights and metrics for tsconfig-paths
Gathering detailed insights and metrics for tsconfig-paths
tsconfig-paths-webpack-plugin
Load modules according to tsconfig paths in webpack.
babel-plugin-tsconfig-paths-module-resolver
A babel plugin that combines babel-plugin-module-resolver and tsconfig-paths to resolve tsconfig paths
rollup-plugin-tsconfig-paths
Rollup plugin for resolving tsconfig paths
typescript-paths
Resolving tsconfig paths in runtime
Load node modules according to tsconfig paths, in run-time or via API.
npm install tsconfig-paths
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,821 Stars
352 Commits
104 Forks
8 Watching
4 Branches
51 Contributors
Updated on 18 Nov 2024
Minified
Minified + Gzipped
TypeScript (99.42%)
JavaScript (0.58%)
Cumulative downloads
Total Downloads
Last day
-2.8%
7,186,645
Compared to previous day
Last week
3.4%
38,464,779
Compared to previous week
Last month
14.9%
156,977,409
Compared to previous month
Last year
29.6%
1,598,160,744
Compared to previous year
Use 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.
Typescript by default mimics the Node.js runtime resolution strategy of modules. But it also allows the use of path mapping 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
.
If 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 to physical file paths that node can load.
yarn add --dev tsconfig-paths
or
npm install --save-dev tsconfig-paths
node -r tsconfig-paths/register main.js
If process.env.TS_NODE_BASEURL
is set it will override the value of baseUrl
in tsconfig.json:
TS_NODE_BASEURL=./dist node -r tsconfig-paths/register main.js
ts-node -r tsconfig-paths/register main.ts
If process.env.TS_NODE_PROJECT
is set it will be used to resolved tsconfig.json
For webpack please use the tsconfig-paths-webpack-plugin.
As of Mocha >= 4.0.0 the --compiler
was deprecated. 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.
1mocha -r ts-node/register -r tsconfig-paths/register "test/**/*.ts"
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.
ts-node
and VSCodeThe following is an example configuration for the .vscode/launch.json
.
1{ 2 "version": "0.2.0", 3 "configurations": [ 4 { 5 "name": "Debug Functions", 6 "request": "launch", 7 "type": "node", 8 "runtimeArgs": [ 9 "-r", 10 "${workspaceFolder}/functions/node_modules/ts-node/register", 11 "-r", 12 "${workspaceFolder}/functions/node_modules/tsconfig-paths/register" 13 ], 14 "args": ["${workspaceFolder}/functions/src/index.ts"], 15 "cwd": "${workspaceFolder}", 16 "protocol": "inspector", 17 "env": { 18 "NODE_ENV": "development", 19 "TS_NODE_PROJECT": "${workspaceFolder}/functions/tsconfig.json" 20 }, 21 "outFiles": ["${workspaceFolder}/functions/lib/**/*.js"] 22 } 23 ] 24}
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.
For example, create a wrapper script called tsconfig-paths-bootstrap.js
with the contents below:
1const tsConfig = require("./tsconfig.json"); 2const tsConfigPaths = require("tsconfig-paths"); 3 4const baseUrl = "./"; // Either absolute or relative path. If relative it's resolved to current working directory. 5const cleanup = tsConfigPaths.register({ 6 baseUrl, 7 paths: tsConfig.compilerOptions.paths, 8}); 9 10// When path registration is no longer needed 11cleanup();
Then run with:
node -r ./tsconfig-paths-bootstrap.js main.js
You can set options by passing them before the script path, via programmatic usage or via environment variables.
1ts-node --project customLocation/tsconfig.json -r tsconfig-paths/register "test/**/*.ts"
Environment variable denoted in parentheses.
-P, --project [path]
Path to TypeScript JSON project file (TS_NODE_PROJECT
)process.env.TS_NODE_PROJECT
to resolve tsConfig.json and the specified baseUrl and paths.The public API consists of these functions:
1export interface ExplicitParams { 2 baseUrl: string; 3 paths: { [key: string]: Array<string> }; 4 mainFields?: (string | string[])[]; 5 addMatchAll?: boolean; 6 cwd?: string; 7} 8 9/** 10 * Installs a custom module load function that can adhere to paths in tsconfig. 11 */ 12export function register(explicitParams: ExplicitParams): () => void;
This function will patch the node's module loading so it will look for modules in paths specified by tsconfig.json
or jsconfig.json
.
A function is returned for you to reinstate Node's original module loading.
1export function loadConfig(cwd: string = process.cwd()): ConfigLoaderResult; 2 3export type ConfigLoaderResult = 4 | ConfigLoaderSuccessResult 5 | ConfigLoaderFailResult; 6 7export interface ConfigLoaderSuccessResult { 8 resultType: "success"; 9 absoluteBaseUrl: string; 10 paths: { [key: string]: Array<string> }; 11} 12 13export interface ConfigLoaderFailResult { 14 resultType: "failed"; 15 message: string; 16}
This 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.
1/** 2 * Function that can match a path 3 */ 4export interface MatchPath { 5 ( 6 requestedModule: string, 7 readJson?: Filesystem.ReadJsonSync, 8 fileExists?: (name: string) => boolean, 9 extensions?: ReadonlyArray<string> 10 ): string | undefined; 11} 12 13/** 14 * Creates a function that can resolve paths according to tsconfig paths property. 15 * @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig. 16 * @param paths The paths as specified in tsconfig. 17 * @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. 18 * @param addMatchAll Add a match-all "*" rule if none is present 19 * @returns a function that can resolve paths. 20 */ 21export function createMatchPath( 22 absoluteBaseUrl: string, 23 paths: { [key: string]: Array<string> }, 24 mainFields: (string | string[])[] = ["main"], 25 addMatchAll: boolean = true 26): MatchPath {
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.
1/** 2 * Finds a path from tsconfig that matches a module load request. 3 * @param absolutePathMappings The paths to try as specified in tsconfig but resolved to absolute form. 4 * @param requestedModule The required module name. 5 * @param readJson Function that can read json from a path (useful for testing). 6 * @param fileExists Function that checks for existence of a file at a path (useful for testing). 7 * @param extensions File extensions to probe for (useful for testing). 8 * @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. 9 * @returns the found path, or undefined if no path was found. 10 */ 11export function matchFromAbsolutePaths( 12 absolutePathMappings: ReadonlyArray<MappingEntry.MappingEntry>, 13 requestedModule: string, 14 readJson: Filesystem.ReadJsonSync = Filesystem.readJsonFromDiskSync, 15 fileExists: Filesystem.FileExistsSync = Filesystem.fileExistsSync, 16 extensions: Array<string> = Object.keys(require.extensions), 17 mainFields: (string | string[])[] = ["main"] 18): string | undefined {
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.
This is the async version of createMatchPath
. It has the same signature but with a callback parameter for the result.
This is the async version of matchFromAbsolutePaths
. It has the same signature but with a callback parameter for the result.
yarn version --patch
yarn version --minor
yarn version --major
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 16/30 approved changesets -- score normalized to 5
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
14 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More