Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
tsconfig-paths
Advanced tools
The tsconfig-paths npm package is used to map module paths based on the paths defined in the tsconfig.json file. This allows TypeScript developers to use custom path mappings to simplify imports in their projects. It is particularly useful for avoiding relative path hell and for making the codebase cleaner and more maintainable.
Custom Path Mapping
This feature allows you to map paths in your TypeScript project so that you can import modules using aliases instead of relative paths. The code sample demonstrates how to register custom path mappings using the tsconfig-paths package.
require('tsconfig-paths').register({ baseUrl: './', paths: { '@app/*': ['./src/app/*'] } });
Integration with Node.js
This feature enables you to integrate tsconfig-paths with Node.js so that when you run your TypeScript-compiled JavaScript files, the custom paths are resolved correctly. The code sample shows how to run a Node.js application with tsconfig-paths support using the -r (require) flag.
node -r tsconfig-paths/register src/server.ts
Command Line Interface
tsconfig-paths provides a CLI tool called tsconfig-paths-bootstrap that can be used to bootstrap the path mappings before running your application. This is useful when you want to ensure that the path mappings are applied before any module resolution takes place.
tsconfig-paths-bootstrap
module-alias is a package that allows you to create aliases for directories and register custom module paths. It is similar to tsconfig-paths but does not rely on the tsconfig.json file for configuration. Instead, you define aliases directly in your package.json or through API calls.
babel-plugin-module-resolver is a Babel plugin that allows you to add custom path resolutions to your project. It is similar to tsconfig-paths in that it helps you avoid relative paths, but it is configured through Babel's configuration file and is typically used in conjunction with Babel's transpilation process.
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.
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 exeute 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
and convert node's module loading calls into to physcial 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
ts-node -r tsconfig-paths/register main.ts
If process.env.TS_NODE_PROJECT
is set it will be used to resolved tsconfig.json
mocha --compilers ts:ts-node/register -r tsconfig-paths/register
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.
const tsConfig = require("./tsconfig.json");
const tsConfigPaths = require("tsconfig-paths");
const baseUrl = "./"; // Either absolute or relative path. If relative it's resolved to current working directory.
tsConfigPaths.register({
baseUrl,
paths: tsConfig.compilerOptions.paths
});
Then run with:
node -r ./tsconfig-paths-bootstrap.js main.js
process.env.TS_NODE_PROJECT
to resolve tsConfig.json and the specified baseUrl and paths.The API consists of these functions:
createMatchPath(absoluteBaseUrl, paths)
This 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 this signature:
(sourceFileName: string, requestedModule: string, readPackageJson: (packageJsonPath: string) => any, fileExists: any, extensions?: Array<string>)
matchFromAbsolutePaths(absolutePathMappings)
Same structure as paths in tsconfig but all paths needs to be resolved to absolute paths. This function is lower level and requries that the paths as already been resolved to absolute form.
FAQs
Load node modules according to tsconfig paths, in run-time or via API.
The npm package tsconfig-paths receives a total of 28,812,149 weekly downloads. As such, tsconfig-paths popularity was classified as popular.
We found that tsconfig-paths demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 14 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.