node.jstypescriptts-node

node-ts shows "error TS2304: Cannot find name '__DEV__'"


I recently added __DEV__ to some TypeScript file in my NodeJS project. In VSCode, this is not marked as an error.

However, when I run the project, I immediately get the error

error TS2304: Cannot find name '__DEV__'.

I tried adding /* global __DEV__ */ to the top of the file. Error still there.

I tried adding a global.d.ts file where I declare var __DEV__: boolean;. Error still there.

Here's my tsconfig:

{
 "compilerOptions": {
  "target": "es6",
  "lib": [
   "es2017","es2015","dom","es6"
  ],
  "module": "commonjs",
  "outDir": "./",
  "sourceMap": true,
  "esModuleInterop": true,
  "strict": false,
  "resolveJsonModule": true,
  "downlevelIteration": true
 },
 "include": [
  "**.ts"
 ],
 "exclude": [
  "node_modules"
 ]
}

EDIT: The project is launched via a launch.json file in VSCode. Here's its contents:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Current TS File",
            "type": "node",
            "request": "launch",
            "args": ["${relativeFile}"],
            "runtimeArgs": ["--nolazy", "-r", "ts-node/register", "--max-old-space-size=32768"],
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
            "internalConsoleOptions": "openOnSessionStart",
            "console": "integratedTerminal",
            "stopOnEntry": false,
            "skipFiles": [
                "${workspaceFolder}/node_modules/**/*.js",
                "<node_internals/**/*.js"
            ]
        }
    ]
}

Solution

  • In the end, the only thing that really worked was moving the __DEV__ variable to an eval:

    const isInDebugMode = () => {
      return eval('__DEV__');
    }
    

    Not ideal, but it did the job.

    The declaration in index.d.ts does only resolve the design-time error. The runtime error is not affected by it.