node.js 获取错误 TS2304:找不到名称“缓冲区”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38875401/
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-09-02 20:29:05  来源:igfitidea点击:

Getting error TS2304: Cannot find name 'Buffer'

node.jstypescripttypestypescript1.8typescript1.4

提问by ayyappa maddi

I am trying to do base64 encode in NodeJS using TypeScript.

我正在尝试使用 TypeScript 在 NodeJS 中进行 base64 编码。

Following code working fine in JavaScript.

以下代码在 JavaScript 中运行良好。

When I am writing same thing in TypeScript and compiling, I am getting Buffer is not find error.

当我在 TypeScript 中编写相同的内容并进行编译时,出现 Buffer is not find 错误。

var base64Policy = new Buffer(stringPolicy, 'utf-8').toString('base64');

Can someone help me to do same thing in TypeScript.

有人可以帮我在 TypeScript 中做同样的事情吗?

回答by DarkKnight

Add this line at top:

在顶部添加此行:

declare const Buffer

and it should compile without errors.

它应该编译没有错误。

Declarations is required to use node built in libraries or other global objects, you can manually declare it like above.

使用节点内置库或其他全局对象需要声明,您可以像上面一样手动声明。

With new version of Typescript, you can also use official declaration files:

使用新版本的 Typescript,您还可以使用官方声明文件:

npm i -g typescript@next
npm i --save-dev @types/node

for other libraries, install @types/library_name.

对于其他库,请安装@types/library_name.

more details: Improve Declaration File Acquisition, The Future of Declaration Files

更多细节:改进声明文件获取声明文件的未来

回答by Benny Neugebauer

Prologue

序幕

Bufferis part of the Node.js API. Because TypeScript doesn't know classes from Node.js by default, you will need to install declaration files(type definitions) for Node.js.

BufferNode.js API 的一部分。因为默认情况下 TypeScript 不知道来自 Node.js 的类,所以您需要为 Node.js安装声明文件(类型定义)。

If you see the following error, you will have to install type definitions manually:

如果您看到以下错误,则必须手动安装类型定义:

error TS2304: Cannot find name 'Buffer'.

错误 TS2304:找不到名称“缓冲区”。

Installing Type Definitions

安装类型定义

You can install type definitions using the typingstool. I will show you how to do this:

您可以使用typings工具安装类型定义。我会告诉你如何做到这一点:

  1. Install the typingstool with npm:

    npm install -g typings

  2. Install type definitions for Node.js from the DefinitelyTyped(~dt) repository:

    typings install dt~node --global --save

  1. typings使用 npm安装该工具:

    npm install -g typings

  2. 绝对类型( ~dt) 存储库安装 Node.js 的类型定义:

    typings install dt~node --global --save

The typings tool will create the following directory "typings/globals/node" and link it in "typings/index.d.ts". There will be also a file called typings.json(because of the --saveoption), which references the resolved type definitions:

打字工具将创建以下目录“ typings/globals/node”并将其链接到“ typings/index.d.ts”中。还将有一个名为typings.json(由于--save选项)的文件,它引用解析的类型定义:

{
  "globalDependencies": {
    "node": "registry:dt/node#6.0.0+20160621231320"
  }
}

Note: If you see the error "typings\globals\node\index.d.ts(71,26): error TS1110: Type expected", then your Node.js definition is too recent. The typings tool has issues with latest type declarations. In such a case, just check the version in your typings.jsonfile. For me node#6.0.0+20160621231320was working but node#6.0.0+20161212163245was not.

注意:如果您看到错误“typings\globals\node\index.d.ts(71,26): error TS1110: Type expected”,那么您的 Node.js 定义太新了。类型工具在最新类型声明方面存在问题。在这种情况下,只需检查typings.json文件中的版本即可。对我来说node#6.0.0+20160621231320是工作但node#6.0.0+20161212163245不是。

  1. Now all you have to do is adding index.d.tsas a triple-slash directivewithin your code (which uses the Bufferclass):
  1. 现在您所要做的就是在您的代码(使用该类)中添加index.d.ts一个三斜杠指令Buffer

YourClass.ts

你的类.ts

/// <reference path="../../typings/index.d.ts" />

export class YourClass {

  private static toString(encoded: string): string {
    return new Buffer(encoded, "base64").toString();
  }

}

UPDATE:

更新:

With the release of TypeScript 2.0a new type definitions system has been announced.

随着TypeScript 2.0发布,发布了一个新的类型定义系统。

You can now forget about the typingstool. All you need to do is running this command to install TypeScript definitions for Node.js:

您现在可以忘记该typings工具了。你需要做的就是运行这个命令来为 Node.js安装TypeScript 定义

npm install --save @types/node

npm install --save @types/node

Please also make sure that you have the following entries in your tsconfig.json:

还请确保您的 中有以下条目tsconfig.json

{
  "compilerOptions": {
    "moduleResolution": "node",
    ...
  },
  "exclude": [
    "node_modules",
    ...
  ]
}

P.S. If you need type definitions for other classes (than included in Node.js), you can search for them here: http://microsoft.github.io/TypeSearch/

PS 如果你需要其他类的类型定义(而不是包含在 Node.js 中),你可以在这里搜索它们:http: //microsoft.github.io/TypeSearch/

回答by Jay Pagnis

For the IONIC 4 developers, I installed it using

npm install --save @types/node

When I tried adding "types": ["node"]as shown below

对于 IONIC 4 开发人员,我使用

npm install --save @types/node

当我尝试添加时安装它"types": ["node"],如下所示

"compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": ["node"]
},

to the

tsconfig.json

it did not work. I had to add the entry to

tsconfig.app.json

Please refer to the following link for relevant documentation.
https://github.com/aws/aws-sdk-js/issues/1271#issuecomment-291658668

Let me know if that worked for you!



tsconfig.json

它没有工作。我不得不将条目添加到

tsconfig.app.json

请参阅以下链接以获取相关文档。
https://github.com/aws/aws-sdk-js/issues/1271#issuecomment-291658668

让我知道这是否对你有用!

回答by abedfar

Buffer is from Nodenamespace. First install

缓冲区来自Node命名空间。首先安装

npm install --save @types/node

npm install --save @types/node

then add below code to your tsconfig.jsonfile inside the compilerOptionssection

然后将以下代码添加到tsconfig.jsoncompilerOptions部分内的文件中

"types": ["node"],
"typeRoots": ["node_modules/@types"]

The typeRootsentry specifies a list of directories for type definition files to be included. It requires TypeScriptversion 2.0 or later.

typeRoots条目指定要包含的类型定义文件的目录列表。它需要TypeScript2.0 或更高版本。