Javascript 如何使用 TypeScript 在 MS 代码中查找模块“fs”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37260901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:06:32  来源:igfitidea点击:

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

javascripttypescripttsc

提问by Brick

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

我在 MacBook Air 上运行。我安装了 MS Code 作为 IDE,还安装了 TypeScript。

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 fswas a core module, so how could it not be found? How do I fix the problem?

我在括号内的“fs”下看到一个红色的波浪线,错误消息是[ts] Cannot find module 'fs'.The file has an .ts extension。我是 JavaScript 和 TypeScript 的新手,但我的印象是它fs是一个核心模块,所以怎么找不到它呢?我该如何解决问题?

Other things that I tried already:

我已经尝试过的其他事情:

  • Putting a simple function body in the file and then compiling on the command line with tsc. I get an essentially equivalent error there: error TS2307: Cannot find module 'fs'.
  • On the command line sudo npm install fs -g. This reports apparent success, but doesn't fix the problem.
  • 将一个简单的函数体放入文件中,然后在命令行上使用tsc. 我在那里得到了一个基本相同的错误:error TS2307: Cannot find module 'fs'.
  • 在命令行上sudo npm install fs -g。这报告了明显的成功,但并没有解决问题。

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

我在 SE 和网络上闲逛,但似乎很接近的答案似乎都假设“fs”可用。

回答by David Sherret

You need to include the definition file for node.

您需要包含节点的定义文件。

TypeScript 2.0+

打字稿 2.0+

Install using npm:

使用 npm 安装:

npm install --save-dev @types/node

TypeScript < 2.0

打字稿 < 2.0

If you use typingsthen you can run this command:

如果您使用打字,那么您可以运行以下命令:

typings install dt~node --global --save

Or if you are using typings < 1.0 run:

或者,如果您正在使用类型 < 1.0 运行:

typings install node --ambient --save

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

或者,如果所有其他方法都失败了,请在此处手动下载文件并将其包含在您的项目中。

回答by Abe

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:

现在有更好的方法,无需使用以前的 tsd 或typings 工具。NPM 现在有用于打字稿的 @types 包。在这个例子中,你需要这个包@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...

确保您使用 save-dev 选项仅在开发模式下安装类型,而不是在生产模式下。使用 npm install "@types/" 语法时,您应该拥有最新的节点库...

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

它没有找到 fs 包,因为以前的工具类型很可能没有使用最新的 node.d.ts 定义文件。

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

您的 tsconfig.json 文件需要更新才能找到这些类型的包。如果使用 jquery、jqueryui 和节点类型,我的示例。假设您还需要使用代码编辑器的语法,在本例中为“原子”代码编辑器

{
"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
}
}

回答by Eric N

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

“fs”是一个核心节点模块,我认为你的导入语句语法有点不对。尝试:

import * as fs from "fs";

回答by yrtimiD

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

您只需要在 tsconfig.json 中将“moduleResolution”设置为“node”:

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

execute

执行

npm install @types/node --save-dev

and now you can use standard TypeScript import:

现在您可以使用标准的 TypeScript 导入:

import * as fs from "fs";

回答by Madhu Ranjan

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

Typescript 了解基于约定的模块,请查看模块分辨率以获取更多详细信息。

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

此外,为了让 IDE 了解fs模块,您必须为节点提供类型。

Also check this github issue

还要检查这个github问题

回答by User Rebo

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

您在tsconfig.json(此处:Node 11+)中有三个选项,请参阅文档

Either specifiyboth typeRootsand types, only typeRootsor removeboth lines completely (recommended):

任一specifiy两个typeRootstypes,仅typeRoots删除完全两条线(推荐):

{"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');
}