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

前端 未结 6 1791
一生所求
一生所求 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 12:50

    "fs" is a core Node module and I think your import statement syntax is a little off. Try:

    import * as fs from "fs";
    
    0 讨论(0)
  • 2021-01-30 12:53

    Typescript knows about modules based upon conventions , check Module resolution for more detail.

    Also for IDE to know about fs module, you have to provide typings for node.

    Also check this github issue

    0 讨论(0)
  • 2021-01-30 12:54

    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.

    0 讨论(0)
  • 2021-01-30 12:55

    There is a better way now without going to the previous tsd or typings tools. NPM now has the @types package for typescript. In this example you need the package @types/node:

    npm install "@types/node" --save-dev
    

    Make sure you are using the save-dev option to only install the types in development mode, not in production. You should have the latest node libraries when use the npm install "@types/" syntax...

    It not finding the fs package because the previous tools typings most likely not using the latest node.d.ts definition file.

    Your tsconfig.json file needs to be updated to find these type packages. My example if using jquery, jqueryui and node types. Assuming you need the syntax to work for your code editor as well, in this case the 'atom' code editor

    {
    "compileOnSave": false,
    "compilerOptions": {
        "rootDir": "src",
        "sourceMap": true,
        "target": "es5",
        "module": "amd",
        "declaration": false,
        "noImplicitAny": false,
        "removeComments": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "moduleResolution": "node",
        "lib": ["es2015", "dom"],
        "baseUrl": "./",
        "typeRoots": [
            "node_modules/@types"
        ],
        "types": [
            "jquery",
            "jqueryui",
            "node"
        ],
        "paths": {
            "src/*": ["src/*"]
        }
    },
    "exclude": [
        "node_modules",
        "dist",
        "build"
    ],
    "filesGlob": [
        "./src/**/*.ts",
        "./test/**/*.ts",
        "./typings/index.d.ts",
        "./custom_typings/**/*.d.ts",
        "./node_modules/@types/**/*.d.ts"
    ],
    "atom": {
        "rewriteTsconfig": false
    }
    }
    
    0 讨论(0)
  • 2021-01-30 13:02

    All you need is "moduleResolution" set to "node" in your tsconfig.json:

    {
      "compilerOptions": {
          ...
          "moduleResolution": "node"
          ...
      }
    }
    

    execute

    npm install @types/node --save-dev
    

    and now you can use standard TypeScript import:

    import * as fs from "fs";
    
    0 讨论(0)
  • 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');
    }
    
    0 讨论(0)
提交回复
热议问题