Can't find the name "Buffer" in ionic app when integrating "amazon-cognito-identity-js"

enter image description here

cannot find name "Buffer" in ionic app while integrating "amazon-cognito-identity-js"

+3


source to share


3 answers


Had a similar problem including amazon-cognito-identity-js

in Angular 4 app. Basically Typescript doesn't understand these Node types (buffer, http, stream, fs, etc). To understand why we need to search for how Typescript specifies packages to include .

By default, all visible "@types" packages are included in your collection. Packages in node_modules / @ types of any closing folder are considered visible; in particular, it means the packages are inside. /node_modules/@types/,../node_modules/@types/,../../node_modules/@types/ etc.

If typesRoots is specified, only packages under typeRoot will be included. eg:

{
   "compilerOptions": {
       "typeRoots" : ["./typings"]
   }
}

      

This config file will include all packages under. / typings and no packages from. / node_modules / @ types.

If types are specified, only the listed packages will be included. For example:

{
   "compilerOptions": {
       "types" : ["node", "lodash", "express"]
   }
}

      



This tsconfig.json file will only include. / node_modules / @ types / node,. / node_modules / @ types / lodash and ./node_modules/@types/express. Other packages in node_modules / @ types / * will not be included.

Hate to copy / paste the main answer, but I can't say it better than the docs.

Check if "typeRoots"

and / or "types"

in any of the files tsconfig.json

(angular-cli have files ./tsconfig.json

and ./app/tsconfig.app.json

).

If "typeRoots"

defined, make sure it is enabled "node_modules/@types"

. If "types"

defined, make sure it is enabled "node"

.

To include all @types, remove "typeRoots"

and "types"

from "compilerOptions"

altogether, but keep in mind that this can create conflicts between declarations of different types, so your best bet is probably to have "node"

included in the property "types"

.

0


source


I found a couple of different things that seemed to contribute to this issue. Thanks to Benny Neugebauer for his response to this post: Error TS2304: Cannot find the name "Buffer"

The keys I found to solve this problem were as follows:

  • Use the stable version of NodeJS, not the newest version

  • Install the typing tool with npm install -g typings

  • Install type definitions using typing install dt ~ node --global --save

  • Make sure these types are not set as they cause problems with the only solution I found to work on both Windows and OSX. npm remove @ types / node

  • Delete the node_modules folder and rebuild with yarn

  • add reference path to app.component.ts

     /// <reference path="../../typings/index.d.ts" />
    
     export class YourClass {
    
          



After that, everything worked. This was the only solution that worked on Windows 10 and OSX environments. My last tsconfig.json file looks like this FYI

   {
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

      

0


source


I found a solution here:

You really need to add "types": ["node"] to the tsconfig.app.json file generated by angular-cli in the src directory. By default, types is an empty array.

https://github.com/aws/aws-sdk-js/issues/1271#issuecomment-291658668

0


source







All Articles