Search code examples
node.jstypescriptvisual-studio-codewindows-subsystem-for-linuxts-node

VS Code reports invalid errors in ts-node shell scripts


I read this article on how to run a typescript file on the command line, and it appears to be working, but VS Code is reporting invalid errors:

enter image description here

You can see from the terminal (bottom half of the picture) that this file runs without issue. According to the lower right-hand corner, VS Code recognizes the file as typescript, but it doesn't recognize any libraries in its environment.

Perhaps it thinks this is configured for web instead of node? I do have evidence for that: it auto-completes the contents of window and document. Is there some way I can tell VS Code that this is a node environment?

It's worth noting that VS Code is running in an Ubuntu WSL terminal/environment on Windows 10. There is no tsconfig.json file involved. It's using whatever's the default. This "script" sits in my personal /bin directory alongside other bash shell scripts. As such, I'm trying to avoid all the boilerplate of setting up an entire TS project to get this working.


Update: That error on fs suggested a "Quick Fix" to install node types. I ran npm i --save-dev @types/node and the error went away, but it created a "node_modules" directory. Since "node_modules" doesn't seem necessary to execute the script, and since it will be used by the next ts shell script I put in that bin/ directory, I'm hoping there is another solution.

I'm trying to avoid creating a project / node_module dir / tsconfig.json / etc. to solve this as my intent is to write a shell script in typescript instead of bash. One of the benefits of shell scripts is you can just create a file and run it; you don't need much, if any, boilerplate.


Solution

  • I don't think you can avoid having to have a node_modules/ or tsconfig.json/jsconfig.json somewhere on your machine. You'll need the node_modules/ if you want to get TypeScript typings (and by extension, typings-related IntelliSense in VS Code) for the NodeJS API, and you'll need the tsconfig.json/jsconfig.json to tell VS Code to understand things like module resolution. They key to the question is how many you can go down to, and where to put it.

    For NodeJS itself, its module resolution logic can be found in the docs, and summarized in the TypeScript module resolution docs, along with the description of TypeScript's module resolution logic. Basically, if you only want to have one node_modules/ and corresponding tsconfig for all you ts-node scripts, then just install @types/node and put your tsconfig.json at the lowest directory that is an ancestor of all your scripts.

    You'll have to open that folder containing the tsconfig.json and node_modules/ as the VS Code workspace folder to get IntelliSense for the scripts within.

    This may not be very ideal depending on where your scripts are all located, but I don't personally know of a better way at the current time.