收到错误 TS2304:找不到名称 'Buffer'

Getting error TS2304: Cannot find name 'Buffer'

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

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

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

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

谁能帮我用 TypeScript 做同样的事情。

在顶部添加这一行:

declare const Buffer

编译应该没有错误。

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

新版Typescript也可以使用官方声明文件:

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

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

更多详情:Improve Declaration File Acquisition, The Future of Declaration Files

序言

Buffer is part of the Node.js API. Because TypeScript doesn't know classes from Node.js by default, you will need to install declaration files(类型定义)用于 Node.js.

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

error TS2304: Cannot find name 'Buffer'.

正在安装类型定义

您可以使用 typings 工具安装类型定义。我将向您展示如何执行此操作:

  1. 使用 npm 安装 typings 工具:

    npm install -g typings

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

    typings install dt~node --global --save

打字工具将在“typings/index.d.ts”中创建以下目录“typings/globals/node”和link。还会有一个名为 typings.json 的文件(因为 --save 选项),它引用已解析的类型定义:

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

注意:如果您看到错误 "typings\globals\node\index.d.ts(71,26): error TS1110: Type expected",那么您的 Node.js 定义太新了。打字工具有 issues with latest type declarations。在这种情况下,只需检查 typings.json 文件中的版本即可。对我来说 node#6.0.0+20160621231320 有效,但 node#6.0.0+20161212163245 无效。

  1. 现在您所要做的就是在代码中添加 index.d.ts 作为 triple-slash directive(使用 Buffer class):

YourClass.ts

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

export class YourClass {

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

}

更新:

随着 release of TypeScript 2.0 新类型定义系统的发布。

您现在可以忘掉 typings 工具了。您需要做的就是 运行 这个命令来安装 TypeScript definitions for Node.js:

npm install --save @types/node

另请确保您的 tsconfig.json 中包含以下条目:

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

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

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

npm install --save @types/node

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

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

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

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

安装它 npm install --save @types/node

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

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



tsconfig.json

它不起作用。我必须将条目添加到

tsconfig.app.json

相关文档请参考以下link
https://github.com/aws/aws-sdk-js/issues/1271#issuecomment-291658668

如果这对您有用,请告诉我!

对于 Angular 用户:

  1. 运行以下命令

npm install --save @types/node

  1. 在您的 tsconfig.app.json 文件中,将这两行添加到 CompilerOptions:
"compilerOptions": {
    //...
    "types": ["node"],
    "typeRoots": ["node_modules/@types"]
  }

要在 Karma/Jasmine

中解决此问题
  1. 运行

npm install --save @types/node

  1. 在您的 tsconfig.spec.json 文件中,将这两行添加到 CompilerOptions:
"compilerOptions": {
    //...
        "types": [ "jasmine" , "node" ],
        "typeRoots": ["node_modules/@types"]
  }