Ts-node: Detect when running in ts-node

Created on 25 Jun 2019  路  12Comments  路  Source: TypeStrong/ts-node

Is there any canonical way for a script to detect when it is running via ts-node?

Sometimes I need to use slightly different paths inside my code (like to reference src/*.ts source, or dist/*.js build files, for example in TypeORM configuration) depending on whether a script is being executed with ts-node or not.

My workaround at the moment is to manually set an environment variable (TS_NODE=true) in my package scripts wherever I run ts-node, but it would be cleaner if this wasn't necessary.

I had a quick look through the source and couldn't see anything obvious e.g. added to global process object or elsewhere that would permit ts-node to be detected but maybe I missed something. If not, perhaps it would be worthwhile to add?

Most helpful comment

I made a PR if you're interested #858

All 12 comments

This sounds reasonable and happy to add it. Where's the most obvious place you'd think of looking? Would it be worthwhile to check by requiring ts-node or do you prefer something in the globals? Do you prefer to have some difference between ts-node CLI and ts-node/register?

Would it be worthwhile to check by requiring ts-node or do you prefer something in the globals?

I'd prefer if detection were possible without requiring ts-node because the checks might occur in a non-development environment where ts-node has not been installed (i.e. when ts-node is in devDependencies only). Also just good to keep the check as lightweight as possible.

The logical place to me would be something on the global process object, whether that's a custom property that ts-node adds at the top level, or nested under somewhere like process.env.

Do you prefer to have some difference between ts-node CLI and ts-node/register?

This wouldn't matter to me, but if it could be useful to others - perhaps whatever property is set could just have different values, or be multiple properties, to discriminate those two cases.

Hello,
I would be very interested in this too, I use express-openapi and the endpoints discovery depends on the folder location from the root of the project (src|dist) and extension (js|ts).

Having the TS_NODE environment variable provided on both ts-node CLI and ts-node/register would be a great thing to be able to change the behavior based on the execution environment.

I made a PR if you're interested #858

Nice solution 馃憣

Thank you @blakeembrey @Nox-404

For anyone looking into this, this is what I got to work for me:

// @ts-ignore
if (process[Symbol.for("ts-node.register.instance")]) {
  process.env.DEV_MODE = "true";
}

I place the above in the entry point of my project and I just reference process.env.DEV_MODE in the rest of the code. I had to use a @ts-ignore though, because I couldn't figure out how to fix this type error:

error TS7053: Element implicitly has an 'any' type because expression of type 'symbol' can't be used to index type 'Process'.

To make things easier for everyone I made a npm package: https://www.npmjs.com/package/detect-ts-node

Does anyone know why this symbol doesn't get set if I run ts-node via an npm script "start": "ts-node src/index.ts",

Whereas it does work if I run ts-node directly

Here is a reproduction showing that the symbol is being set in both cases. It may help you debug.
https://github.com/TypeStrong/ts-node-repros/tree/3527
https://github.com/TypeStrong/ts-node-repros/runs/2325732351?check_suite_focus=true#step:7:40

@mmeinzer you try to check the version of ts-node inside of the node_modules. I hit same issue, because typeorm's project generator uses outdated version of "ts-node"

Was this page helpful?
0 / 5 - 0 ratings