Ts-node: Possible to `node -r ts-node/register` and specify the project?

Created on 15 Nov 2018  Â·  8Comments  Â·  Source: TypeStrong/ts-node

i need to specify --max-old-space-size, which forces me to use node -r, but i have a non-default tsconfig.server.json file to use as the config insead of the default. How am i supposed to do that?

question

Most helpful comment

Care to elaborate?

All 8 comments

Use environment variables.

Care to elaborate?

On what?

Reference: https://github.com/TypeStrong/ts-node#cli-and-programmatic-options

Ahhh. Thank you. I completely missed these in the docs.

No problem, I feel like we should try and add more examples using the environment variables to the README - are you familiar with examples on Windows?

@blakeembrey I agree that example usage of environment variables would be hugely helpful.

I'm invoking ts-node like this:

/usr/bin/nodejs \
--inspect \
--require ts-node/register \
src/index.ts \
--production \
--port=3004

... However, I don't know how to pass environment variables into ts-node via this invocation. I've been searching the repo and Google for nearly 30 minutes now trying to get even one example. It's pretty gruelling.

@shirakaba What are you searching with exactly? I'm curious what issue you're running into.

The reason this hasn't been well-documented before is because environment variables are a common concept across all OSes and projects. It's different for Unix vs Windows, and different in editors such as VS Code (with launch.json), Node.js, Python, etc. Each have conventions for setting and getting them. That said, it would be very beneficial to link to a central place for documentation on using environment variables on any platform.

Edit: Here's an example via CLI - TS_NODE_PROJECT=tsconfig.server.json node -r ts-node/register.

Applying ts-node environment variables via shell

@blakeembrey Here is the approach I was taking:

Not working

TS_NODE_FILES="true" \
&& /usr/bin/nodejs \
--inspect \
--require ts-node/register \
src/index.ts \
--production \
--port=3004

I had mistakenly assumed that environment variables would carry past the &&.

Thank you immensely for that example. Simply removing the && does the trick:

Working

TS_NODE_FILES="true" \
/usr/bin/nodejs \
--inspect \
--require ts-node/register \
src/index.ts \
--production \
--port=3004

This may be intuitive to those familiar with shells, but I feel that a large proportion of users of ts-node may face friction in getting their environment variables set up, so a little bit of documentation on the fundamentals may encourage users to use non-default configurations of ts-node.

Applying ts-node environment variables in the context of Mocha

Off-topic, but if anyone ends up here and is using ts-node for Mocha, then here is one way to pass environment variables:

test/mocha.opts

--require test/mocha.env
--require ts-node/register
--require test/setup
--debug

test/mocha.env.js

// From: https://github.com/mochajs/mocha/issues/185#issuecomment-321566188

/*
 * Force ts-node to use TypeScript module resolution as an inefficient
 * (yet easy – i.e. without changing original TypeScript project's
 * directory structure) way to take notice of ambient .d.ts files
*/
process.env.TS_NODE_FILES = "true";

test/setup.ts

// Run any pre-test setup like polyfills

test/MyTest.spec.ts

import {expect} from "chai";

describe('promiseFunnel module', function() {
    it('Example Promisified test', function () {
        return Promise.resolve();
    });
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

cevek picture cevek  Â·  4Comments

nehalist picture nehalist  Â·  3Comments

remojansen picture remojansen  Â·  4Comments

OliverJAsh picture OliverJAsh  Â·  3Comments

mattdell picture mattdell  Â·  4Comments