ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

Detect when running in ts-node

Open sgarner opened this issue 7 years ago • 13 comments

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?

sgarner avatar Jun 25 '19 05:06 sgarner

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?

blakeembrey avatar Jul 01 '19 01:07 blakeembrey

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.

sgarner avatar Jul 03 '19 00:07 sgarner

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.

nox-404 avatar Jul 26 '19 09:07 nox-404

I made a PR if you're interested #858

nox-404 avatar Jul 26 '19 09:07 nox-404

Closed with https://github.com/TypeStrong/ts-node/commit/b681e2457c7acc28fcbba638a8dac6ecf27846e3.

blakeembrey avatar Nov 11 '19 02:11 blakeembrey

Nice solution 👌

Thank you @blakeembrey @Nox-404

sgarner avatar Nov 11 '19 02:11 sgarner

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'.

adrianmcli avatar May 21 '20 01:05 adrianmcli

@adrianmcli You'd need something like this: https://github.com/TypeStrong/ts-node/blob/3978f328a3970cd4f81f6a1ad1ed9d5c31fad8c0/src/index.ts#L35-L41.

blakeembrey avatar May 21 '20 01:05 blakeembrey

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

yorickdevries avatar Sep 13 '20 21:09 yorickdevries

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

mmeinzer avatar Apr 12 '21 16:04 mmeinzer

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

cspotcode avatar Apr 12 '21 16:04 cspotcode

@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"

zinoviev avatar Apr 13 '21 17:04 zinoviev

hello, still no native way to detect ts-node ?

kopax avatar Aug 08 '24 12:08 kopax