Barcode Scanner causes compile errors when importing into an Ionic 4 app.
ionic start junk blank --type=angular
npm install @ionic-native/barcode-scanner
add import to home.page.ts
import { Component } from '@angular/core';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
constructor(private barcodeScanner: BarcodeScanner) {}
}
ionic build --prod gives this error:
ERROR in @ionic-native\barcode-scanner\index.ts(104,2): Error during template compile of ‘BarcodeScanner’
Only initialized variables and constants can be referenced in decorators because the value of this variable is needed by the template compiler in ‘Plugin’
‘Plugin’ references ‘Plugin’
‘Plugin’ is not initialized at @ionic-native\core\decorators\interfaces.ts(100,22).
ionic serve crashes with this error in console:
core.js:1673 ERROR Error: Uncaught (in promise): TypeError: Object(…) is not a function
TypeError: Object(…) is not a function
at index.js:106
at Object…/node_modules/@ionic-native/barcode-scanner/index.js (index.js:155)
at webpack_require (bootstrap:81)
at Object…/src/app/home/home.page.ts (home-home-module.js:262)
at webpack_require (bootstrap:81)
at Object…/src/app/home/home.module.ts (home-home-module.js:190)
at webpack_require (bootstrap:81)
at $_lazy_route_resource lazy namespace object:17
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:3824)
at index.js:106
at Object…/node_modules/@ionic-native/barcode-scanner/index.js (index.js:155)
at webpack_require (bootstrap:81)
at Object…/src/app/home/home.page.ts (home-home-module.js:262)
at webpack_require (bootstrap:81)
at Object…/src/app/home/home.module.ts (home-home-module.js:190)
at webpack_require (bootstrap:81)
at $_lazy_route_resource lazy namespace object:17
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:3824)
at resolvePromise (zone.js:814)
at resolvePromise (zone.js:771)
at zone.js:873
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3815)
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push…/node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:595)
ERROR in @ionic-native\barcode-scanner\index.ts(104,2): Error during template compile of ‘BarcodeScanner’
Only initialized variables and constants can be referenced in decorators because the value of this variable is needed by the template compiler in ‘Plugin’
‘Plugin’ references ‘Plugin’
‘Plugin’ is not initialized at @ionic-native\core\decorators\interfaces.ts(100,22).
[ERROR] An error occurred while running subprocess ng.
ng run app:ionic-cordova-build:production --platform=android exited with exit code 1.
Re-running this command with the --verbose flag may provide more information.
I also found this error from Chrome DevTools’ JavaScript console while running this app on Android:
Uncaught TypeError: Object(...) is not a function
at vendor.js:64108
at Object../node_modules/@ionic-native/barcode-scanner/index.js (vendor.js:64157)
at __webpack_require__ (runtime.js:82)
at Object../src/app/app.module.ts (main.js:156)
at __webpack_require__ (runtime.js:82)
at Object../src/main.ts (main.js:243)
at __webpack_require__ (runtime.js:82)
at Object.0 (main.js:265)
at __webpack_require__ (runtime.js:82)
at checkDeferredModules (runtime.js:44)
I don’t sure that should barcode scanner plugin do some update like SplashScreen did, according to this issue.
Noted that SplashScreen plugin seems got updated import statement:
import {SplashScreen} from '@ionic-native/splash-screen/ngx';
Honestly, I found one of your link and I also found out that we also need to update version of @ionic-native/barcode-scanner to match @ionic/core version.
For anyone who’re encountering with this issue. The installation command on beta (and also in v3) documentation will cause the problem:
npm install --save @ionic-native/barcode-scanner
This will led to version @ionic-native/barcode-scanner: "^4.12.0" in package.json. This leads to the error in this issue.
The command with specified version will give you correct version: