How to find module “fs” in VS Code with TypeScript?

前端 未结 6 1907
一生所求
一生所求 2021-01-30 12:17

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 = req         


        
6条回答
  •  忘了有多久
    2021-01-30 13:07

    You have three options in tsconfig.json (here: Node 11+), see docs:

    Either specifiy both typeRoots and types, only typeRoots or remove both lines completely (recommended):

    {"compilerOptions": {
      ...
      "typeRoots": ["node_modules/@types"],  // "typeRoots": [] -> won't work
      "types": ["node"]                      // "types": [] -> won't work
    }}
    

    Install types:

    npm install --save-dev @types/node
    

    Use promise based file system (e.g to use async / await):

    import {promises as fs} from 'fs';
    
    async function foo() {
        ...
        await fs.writeFile('./yourPath', 'YourContent');
    }
    

提交回复
热议问题