javascripttypescripttsc

How to find module "fs" in VS Code with TypeScript?


I'm running on a MacBook Air. I installed VS Code as an IDE and also have TypeScript installed.

I have a simple file with just this line:

import fs = require('fs');

I'm getting a red squiggly under the 'fs' inside the parenthesis and the error message is [ts] Cannot find module 'fs'. The file has a .ts extension. I'm new to JavaScript and to TypeScript, but I was under the impression that fs was a core module, so how could it not be found? How do I fix the problem?

Other things that I tried already:

I poked around SE and the web, but the answers that seemed close all appear to assume that 'fs' is available.


Solution

  • You need to include the definition file for node.

    TypeScript 2.0+

    Install using npm:

    npm install --save-dev @types/node
    

    TypeScript < 2.0

    If you use typings then you can run this command:

    typings install dt~node --global --save
    

    Or if you are using typings < 1.0 run:

    typings install node --ambient --save
    

    Or if all else fails, manually download the file here and include it in your project.