Hassan Khan
Hassan Khan

Reputation: 11

Cannot find name 'NDEFReader' in Angular 9

I'm trying to integrate WebNFC in my angular project, I found only package is @types/w3c-web-nfc but when I run application it shows me the error mentioned below:

error TS2304: Cannot find name 'NDEFReader'.
    
    49       const ndef = new NDEFReader();

I've checked few resources that says with ssl it would work but I tried it's not working.

Any help would be highly appreciated!

Upvotes: 1

Views: 140

Answers (1)

Morph
Morph

Reputation: 83

Angular for building use tsconfig.app.json , you can see it in angular.json file:

"architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
           // ...
            "tsConfig": "tsconfig.app.json",
           // ...
}

where change compilerOptions , by adding "types": [] , empty array in this option meaning that automatic inclusion of all packages in node_modules/@types has been disabled.

You have too options:

  1. Change tsconfig.app.json by adding value to "types" like this:
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": ["w3c-web-nfc"]    // <==== Tell to include this types in the global scope
  },
  "files": ["src/main.ts", "src/polyfills.ts"],
  "include": ["src/**/*.d.ts"]
}
  1. You can copy types manually from w3c-web-nfc to, for example, src/@types/nfc-web.d.ts in you project and angular automatically take them. Because of "include": ["src/**/*.d.ts"] parameter in tsconfig.app.json

Choose prefer way for you.

Upvotes: 0

Related Questions