Detect if Running in TS Node

1.0.5 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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();

view raw JSON →