Registry / testing / detect-ts-node

detect-ts-node

JSON →
library1.0.5jsnpmunverified

The `detect-ts-node` package is a utility designed to programmatically determine if the currently executing JavaScript or TypeScript code is running within a `ts-node` environment. It provides a single boolean export that is `true` when `ts-node` is active and `false` otherwise. The current stable version is 1.0.5. Given its highly focused and stable nature, the package likely maintains a very low release cadence, with updates primarily addressing internal `ts-node` changes or minor bug fixes. A key differentiator is its simplicity and direct implementation, based on established discussions within the `ts-node` community, making it a reliable choice for developers who need to implement conditional logic, configuration adjustments, or specific debugging behaviors when operating under `ts-node` versus a pre-compiled JavaScript environment. It explicitly targets `ts-node` detection, distinguishing it from broader TypeScript compilation environment checks.

npm install detect-ts-node
INSTALL
IMPORT
SIG · DETECT-TS-NODE
D
detect-ts-node
testingjavascriptv1.0.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

detectTSNode
import detectTSNode from 'detect-ts-node';
import { detectTSNode } from 'detect-ts-node';
The package exports a single boolean value as its default export. Attempting to use named import syntax for `detectTSNode` will result in a runtime error or a build error.
detectTSNode
const detectTSNode = require('detect-ts-node');
const detectTSNode = require('detect-ts-node').default;
In CommonJS environments, `require('detect-ts-node')` directly resolves to the default export. Explicitly accessing `.default` is unnecessary and can be incorrect if the environment does not correctly interop ESM default exports.
type of detectTSNode
// No specific type import is needed, as `detectTSNode` is a boolean value. // TypeScript's type inference handles it automatically.
While the package ships TypeScript types, `detectTSNode` itself is a simple boolean (`boolean` type). No dedicated type import is required or available for this value.

Demonstrates how to import and use the `detectTSNode` boolean value to conditionally execute logic or set configurations based on whether the code is running within a `ts-node` environment.

import detectTSNode from "detect-ts-node"; function initializeApplication() { if (detectTSNode) { console.log("TS Node detected. Applying development-specific configurations."); // Example: Dynamically load '.env.development' or enable hot-reloading process.env.NODE_ENV = 'development'; // Potentially load a different config module // const config = require('./config.ts-node').default; } else { console.log("Not running in TS Node. Standard production or compiled JS environment."); // Example: Load '.env.production' or rely on pre-compiled assets process.env.NODE_ENV = 'production'; // const config = require('./config.js').default; } console.log(`Application environment set to: ${process.env.NODE_ENV}`); } initializeApplication();
Debug
Known issues
gotchaThe detection mechanism relies on internal heuristics that `ts-node` exposes. While generally robust, future major versions of `ts-node` could theoretically change these internals, potentially leading to incorrect detection in `detect-ts-node` without a corresponding package update.
fix
Always test `detect-ts-node` against new major `ts-node` versions if its accuracy is critical to your application's conditional logic. Monitor the `detect-ts-node` GitHub repository for updates addressing `ts-node` changes.
affects: >=1.0.0
gotchaThis package is specifically designed to detect `ts-node`. It does not detect other TypeScript compilation environments such as `ts-jest`, `ts-loader` for Webpack, `esbuild` with TypeScript, or direct `tsc` compilation. Its scope is limited to `ts-node`.
fix
If you need to detect broader TypeScript compilation contexts, implement custom checks based on specific environment variables (e.g., `TS_JEST` for Jest) or specific build tool flags that indicate their presence.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0, _detectTsNode.default) is not a function
Attempting to call `detectTSNode` as a function when it is a boolean value, or incorrect CommonJS interop with ESM default exports.
fix
Ensure you are treating `detectTSNode` as a boolean (`if (detectTSNode) { ... }`) and not invoking it. For CommonJS, use `const detectTSNode = require('detect-ts-node');` directly without `.default` if you encounter this.
SyntaxError: Named export 'detectTSNode' not found. Did you mean 'default'?
Attempting to import the default export using named import syntax in an ESM module.
fix
Change `import { detectTSNode } from 'detect-ts-node';` to `import detectTSNode from 'detect-ts-node';` to correctly import the default export.
Upgrade
Version history
1.0.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
detect-ts-node — npm install detect-ts-node · libregistry