Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
get-tsconfig
Advanced tools
The get-tsconfig npm package is a utility for finding and reading TypeScript configuration files (tsconfig.json). It simplifies the process of locating the tsconfig.json file and parsing its contents, making it easier to work with TypeScript configurations programmatically.
Find the tsconfig.json file
This feature allows you to locate the tsconfig.json file in your project. The `find` function searches for the tsconfig.json file starting from the current directory and moving up the directory tree until it finds one.
const { find } = require('get-tsconfig');
const tsconfigPath = find();
console.log(tsconfigPath);
Read and parse the tsconfig.json file
This feature allows you to read and parse the contents of the tsconfig.json file. The `load` function returns the parsed JSON content of the tsconfig.json file, making it easy to access and manipulate the TypeScript configuration programmatically.
const { load } = require('get-tsconfig');
const tsconfig = load();
console.log(tsconfig);
Specify a custom path to the tsconfig.json file
This feature allows you to specify a custom path to the tsconfig.json file. The `load` function can take a file path as an argument, enabling you to load and parse a tsconfig.json file from a specific location.
const { load } = require('get-tsconfig');
const tsconfig = load('/path/to/your/tsconfig.json');
console.log(tsconfig);
The tsconfig-paths package helps resolve TypeScript paths in Node.js. It reads the paths and baseUrl options from the tsconfig.json file and configures the module resolution accordingly. Unlike get-tsconfig, which focuses on finding and reading the tsconfig.json file, tsconfig-paths is more about resolving module paths based on the TypeScript configuration.
The typescript package itself provides the TypeScript compiler and related tools, including the ability to read and parse tsconfig.json files. While it offers more comprehensive functionality for working with TypeScript, it is more complex and heavier compared to the lightweight and focused get-tsconfig package.
The cosmiconfig package is a universal configuration loader that supports various configuration file formats, including JSON, YAML, and more. It can be used to find and load configuration files like tsconfig.json, but it is more general-purpose and not specifically tailored for TypeScript configurations like get-tsconfig.
Find and parse tsconfig.json
files.
tsconfig.json
extends
tsconfig.json
3 kB
Minified + Gzippednpm install get-tsconfig
For TypeScript related tooling to correctly parse tsconfig.json
file without depending on TypeScript.
import getTsconfig from 'get-tsconfig'
// Finds tsconfig.json in the current directory
console.log(getTsconfig())
// Find tsconfig.json from a TypeScript file path
console.log(getTsconfig('./path/to/index.ts'))
// Find tsconfig.json from a directory file path
console.log(getTsconfig('./path/to/directory'))
// Explicitly pass in tsconfig.json path
console.log(getTsconfig('./path/to/tsconfig.json'))
Searches for a tsconfig.json
file and parses it. Returns null
if a config file cannot be found, or an object containing the path and parsed TSConfig object if found.
Returns:
type TsconfigResult = {
/**
* The path to the tsconfig.json file
*/
path: string
/**
* The resolved tsconfig.json file
*/
config: TsConfigJsonResolved
}
Type: string
Default: process.cwd()
Accepts a path to a file or directory to search up for a tsconfig.json
file.
Type: string
Default: tsconfig.json
The file name of the TypeScript config file.
Given a tsconfig with compilerOptions.paths
defined, it returns a matcher function.
import getTsconfig from 'get-tsconfig'
import { createPathsMatcher } from 'get-tsconfig/paths'
const tsconfig = getTsconfig()
const pathsMatcher = createPathsMatcher(tsconfig)
The matcher function accepts an import specifier (the path to resolve), checks it against compilerOptions.paths
, and returns an array of possible paths to check:
function pathsMatcher(specifier: string): string[]
This function only returns possible paths and doesn't actually do any resolution. This helps increase compatibility wtih file/build systems which usually have their own resolvers.
tsconfig.json
?This package is a re-implementation of TypeScript's tsconfig.json
parser.
However, if you already have TypeScript as a dependency, you can simply use it's API:
import {
sys as tsSys,
findConfigFile,
readConfigFile,
parseJsonConfigFileContent
} from 'typescript'
// Find tsconfig.json file
const tsconfigPath = findConfigFile(process.cwd(), tsSys.fileExists, 'tsconfig.json')
// Read tsconfig.json file
const tsconfigFile = readConfigFile(tsconfigPath, tsSys.readFile)
// Resolve extends
const parsedTsconfig = parseJsonConfigFileContent(
tsconfigFile.config,
tsSys,
path.dirname(tsconfigPath)
)
FAQs
Find and parse the tsconfig.json file from a directory path
The npm package get-tsconfig receives a total of 10,347,964 weekly downloads. As such, get-tsconfig popularity was classified as popular.
We found that get-tsconfig demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.