0

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"
            ]
        }
    ]
}
Jonas Sourlier
  • 13,684
  • 16
  • 77
  • 148
  • 1
    You left out the most important information. _How_ are you running the project? What arguments are you passing to `ts-node`? Also, your `tsconfig.json` has a syntax error because it isn't valid JSON which will cause some tools to fail. – Aluan Haddad Sep 07 '20 at 12:52
  • 1
    @AluanHaddad See edits. `tsconfig.json` should now be valid json. – Jonas Sourlier Sep 07 '20 at 14:29

2 Answers2

2

There is a caveat regarding to typing which is officially expressed on the repo https://github.com/TypeStrong/ts-node#help-my-types-are-missing.

To sump up, you can resolve the problem by doing things as following:

Create the structure for typings dir like this:

- tsconfig.json
- typings
-- global
--- index.d.ts

with the index.d.ts is your content:

declare var __DEV__: boolean

Then add typeRoots to your tsconfig.json:

{
  "compilerOptions": {
    "typeRoots" : ["./node_modules/@types", "./typings"]
  }
}

tmhao2005
  • 14,776
  • 2
  • 37
  • 44
  • 1
    This seems to work. However, it does not answer why `__DEV__` is missing at all. Should not `__DEV__` be a defined variable in all NodeJS environments? – Jonas Sourlier Sep 08 '20 at 09:02
  • I don't think so that var is defined by default. I would say you can use `process.env.NODE_ENV` instead – tmhao2005 Sep 08 '20 at 09:03
0

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.

Jonas Sourlier
  • 13,684
  • 16
  • 77
  • 148