Getting error TS2304: cannot find name 'Buffer'

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

The following code works fine in JavaScript.

When I write the same in TypeScript and compile, I get a Buffer not find error message.

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

Can someone help me do the same in TypeScript.

+20
types typescript
source share
3 answers

Add this line at the top:

 declare const Buffer 

and it should compile without errors.

Declarations must use node built-in libraries or other global objects, you can manually declare them as described above.

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

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

for other libraries, set @types/library_name .

more: Improve the collection of document files , the future of declaration files

+34
source share

Prologue

Buffer is part of the Node.js API . Since TypeScript does not know the classes from Node.js by default, you will need to install declaration files (type definitions) for Node.js.

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

Error TS2304: Cannot find the name "Buffer".

Setting Type Definitions

You can set type definitions using the typings tool. I will show you how to do it:

  • Install the typings tool with npm:

    npm install -g typings

  • Set the type definitions for Node.js from the DefinitelyTyped ( ~dt ) repository:

    typings install dt~node --global --save

The Typing tool will create the following typings/globals/node directory and link it to typings/index.d.ts . There will also be a file called typings.json (due to the --save parameter) that references the definitions of allowed types:

 { "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 large. The typing tool has problems with the latest type declarations . In this case, just check the version in the typings.json file. For me, node#6.0.0+20160621231320 worked, but node#6.0.0+20161212163245 not.

  1. Now you need to add index.d.ts as a triple-slash directive to your code (which uses the 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(); } } 

UPDATE:

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

Now you can forget about the typings tool. All you have to do is run this command to set the TypeScript definitions for Node.js :

npm install --save @types/node

Also make sure you have the following entries in tsconfig.json :

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

PS If you need type definitions for other classes (than in Node.js), you can find them here: http://microsoft.imtqy.com/TypeSearch/ p>

+20
source share

A buffer from the Node namespace. First install

npm install --save @types/node

then add the code below to your tsconfig.json file in the compilerOptions section

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

The typeRoots defines a list of directories for the included type definition files. TypeScript version 2.0 or higher is required.

+5
source share

All Articles