Ts-node: bug with const enum

Created on 10 Mar 2019  Â·  5Comments  Â·  Source: TypeStrong/ts-node

run .ts with ts-node

index.ts:29
        if (realpath === getCWD.EnumRealPath.FS)
                                       ^
TypeError: Cannot read property 'FS' of undefined
    at getCWD (index.ts:29:40)
    at Object.<anonymous> (index.ts:57:13)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Module.m._compile (C:\Users\User\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:414:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\User\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:417:12)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)

ts file

import fs = require('fs');
import path = require('path');

export function getCWD(cwd?: string, realpath?: boolean | getCWD.EnumRealPath, failback?: string | (() => string)): string
{
    if (notEmptyString(cwd))
    {
        cwd = cwd.trim();
    }
    else if (typeof failback === 'function')
    {
        cwd = failback();
    }
    else if (notEmptyString(failback))
    {
        cwd = failback;
    }
    else if (cwd == null)
    {
        return process.cwd()
    }
    else
    {
        cwd = undefined;
    }

    if (realpath && cwd != null)
    {
        if (realpath === getCWD.EnumRealPath.FS)
        {
            return fs.realpathSync(cwd);
        }

        return path.resolve(cwd);
    }

    return cwd
}

export declare namespace getCWD
{
    export const enum EnumRealPath
    {
        NONE = 0,
        FS = 1,
        PATH = 2
    }
}

export function notEmptyString(s: string)
{
    return typeof s === 'string' && s.trim() !== ''
}

export default exports as typeof import('./index');

console.log(getCWD(null, true, 'll'));

typescript output js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
function getCWD(cwd, realpath, failback) {
    if (notEmptyString(cwd)) {
        cwd = cwd.trim();
    }
    else if (typeof failback === 'function') {
        cwd = failback();
    }
    else if (notEmptyString(failback)) {
        cwd = failback;
    }
    else if (cwd == null) {
        return process.cwd();
    }
    else {
        cwd = undefined;
    }
    if (realpath && cwd != null) {
        if (realpath === 1 /* FS */) {
            return fs.realpathSync(cwd);
        }
        return path.resolve(cwd);
    }
    return cwd;
}
exports.getCWD = getCWD;
function notEmptyString(s) {
    return typeof s === 'string' && s.trim() !== '';
}
exports.notEmptyString = notEmptyString;
exports.default = exports;
console.log(getCWD(null, true, 'll'));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJpbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHlCQUEwQjtBQUMxQiw2QkFBOEI7QUFFOUIsU0FBZ0IsTUFBTSxDQUFDLEdBQVksRUFBRSxRQUF3QyxFQUFFLFFBQWtDO0lBRWhILElBQUksY0FBYyxDQUFDLEdBQUcsQ0FBQyxFQUN2QjtRQUNDLEdBQUcsR0FBRyxHQUFHLENBQUMsSUFBSSxFQUFFLENBQUM7S0FDakI7U0FDSSxJQUFJLE9BQU8sUUFBUSxLQUFLLFVBQVUsRUFDdkM7UUFDQyxHQUFHLEdBQUcsUUFBUSxFQUFFLENBQUM7S0FDakI7U0FDSSxJQUFJLGNBQWMsQ0FBQyxRQUFRLENBQUMsRUFDakM7UUFDQyxHQUFHLEdBQUcsUUFBUSxDQUFDO0tBQ2Y7U0FDSSxJQUFJLEdBQUcsSUFBSSxJQUFJLEVBQ3BCO1FBQ0MsT0FBTyxPQUFPLENBQUMsR0FBRyxFQUFFLENBQUE7S0FDcEI7U0FFRDtRQUNDLEdBQUcsR0FBRyxTQUFTLENBQUM7S0FDaEI7SUFFRCxJQUFJLFFBQVEsSUFBSSxHQUFHLElBQUksSUFBSSxFQUMzQjtRQUNDLElBQUksUUFBUSxlQUEyQixFQUN2QztZQUNDLE9BQU8sRUFBRSxDQUFDLFl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wcm9jZXNzLmN3ZCgpXG5cdH1cblx0ZWxzZVxuXHR7XG5cdFx0Y3dkID0gdW5kZWZpbmVkO1xuXHR9XG5cblx0aWYgKHJlYWxwYXRoICYmIGN3ZCAhPSBudWxsKVxuXHR7XG5cdFx0aWYgKHJlYWxwYXRoID09PSBnZXRDV0QuRW51bVJlYWxQYXRoLkZTKVxuXHRcdHtcblx0XHRcdHJldHVybiBmcy5yZWFscGF0aFN5bmMoY3dkKTtcblx0XHR9XG5cblx0XHRyZXR1cm4gcGF0aC5yZXNvbHZlKGN3ZCk7XG5cdH1cblxuXHRyZXR1cm4gY3dkXG59XG5cbmV4cG9ydCBkZWNsYXJlIG5hbWVzcGFjZSBnZXRDV0Rcbntcblx0ZXhwb3J0IGNvbnN0IGVudW0gRW51bVJlYWxQYXRoXG5cdHtcblx0XHROT05FID0gMCxcblx0XHRGUyA9IDEsXG5cdFx0UEFUSCA9IDJcblx0fVxufVxuXG5leHBvcnQgZnVuY3Rpb24gbm90RW1wdHlTdHJpbmcoczogc3RyaW5nKVxue1xuXHRyZXR1cm4gdHlwZW9mIHMgPT09ICdzdHJpbmcnICYmIHMudHJpbSgpICE9PSAnJ1xufVxuXG5leHBvcnQgZGVmYXVsdCBleHBvcnRzIGFzIHR5cGVvZiBpbXBvcnQoJy4vaW5kZXgnKTtcblxuY29uc29sZS5sb2coZ2V0Q1dEKG51bGwsIHRydWUsICdsbCcpKTtcbiJdfQ==
external invalid

Most helpful comment

A note for anyone like me stuck on that: using just enum instead of const enum makes it work with ts-node. The purpose of const part is exactly to hide the enum names from the runtime.

All 5 comments

@bluelovers How are you running ts-node?

ts-node .ts file lol

Hey, I can reproduce this issue if I set "TS_NODE_TRANSPILE_ONLY=1".

I had the same issue "cannot read" for const enum (with TS_NODE_TRANSPILE_ONLY=1) for circular dependencies:

mytest.test.ts imports a.ts, a.ts imports a const enum from b.ts, but b.ts imports something from a.ts -> error in a.ts.

I don't know if I would call it a bug, since I think it's bad design to have circular dependencies anyway — next step for me is to see if I can get some linting rule to forbid circular dependencies.

One thing ts-node _could_ do, is to update the documentation that TS_NODE_TRANSPILE_ONLY breaks if you have circular dependencies. If @blakeembrey agrees I'm happy to provide a PR for that.

Const enums wouldn’t work as expected in transpile mode, it’s one of the things that requires types to replace usages. I thought there were docs on that, but I don’t see it now. There’s past issues around this too that should be searchable and I’m happy to add docs back.

A note for anyone like me stuck on that: using just enum instead of const enum makes it work with ts-node. The purpose of const part is exactly to hide the enum names from the runtime.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

remojansen picture remojansen  Â·  4Comments

sodiumjoe picture sodiumjoe  Â·  4Comments

aj-r picture aj-r  Â·  3Comments

huan picture huan  Â·  3Comments

joshua-tj picture joshua-tj  Â·  3Comments