Socket
Socket
Sign inDemoInstall

tsconfig-paths

Package Overview
Dependencies
8
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    tsconfig-paths

Load node modules according to tsconfig paths, in run-time or via API.


Version published
Maintainers
1
Install size
132 kB
Created

Package description

What is tsconfig-paths?

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.

What are tsconfig-paths's main functionalities?

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

Other packages similar to tsconfig-paths

Changelog

Source

[0.4.0] - 2016-12-30

Changed

  • Renamed project to tsocnfig-paths.

Readme

Source

tsconfig-paths

NPM version

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.

How to install

yarn add --dev tsconfig-paths

or

npm install --save-dev tsconfig-paths

How to use

With node

node -r tsconfig-paths/register main.js

With ts-node

ts-node -r tsconfig-paths/register main.ts

With mocha and ts-node

mocha --compilers ts:ts-node/register -r tsconfig-paths/register

Programmatic use

The API consists of these functions:

createMatchPath(tsConfigPath, baseUrl, 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: matchPath(absoluteSourceFileName: string, requestedModule: string)

matchFromAbsolutePaths(absolutePaths, absoluteSourceFileName, requestedModule)

This function is lower level and requries that the paths as already been resolved to absolute form.

FAQs

Last updated on 30 Dec 2016

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc