don / cordova-plugin-ble-central

Bluetooth Low Energy (BLE) Central plugin for Apache Cordova (aka PhoneGap)
Apache License 2.0
941 stars 601 forks source link

Module not found. Can't resolve 'cordova-plugin-ble-central'. (TS) #1007

Closed fudom closed 2 months ago

fudom commented 2 months ago

I get an error during build when

import 'cordova-plugin-ble-central';

No error in VSCode. But the Angular build throws:

Error: Module not found: Error: Can't resolve 'cordova-plugin-ble-central'

Has anyone tested this? I'm not sure what's wrong in my tsconfig.json. Great that types are present. But I would add this to the docs or fix that. It seems that the types.d.ts extends window with ble. It's not a module. Fine, but why this error? The package.json looks ok. I'm currently exhausted and appreciate any hint.

peitschie commented 2 months ago

Hi @fudom

Cordova plugins are not directly included via ES6 imports however. The javascript itself is automatically packed in via the cordova CLI tool, so attempting to directly include like this would likely just create a duplicate import and possibly some plugin confusion.

I generally reference these via triple-slash directives:

/// <reference types="cordova-plugin-ble-central" />

You can also add these to the types collection in your tsconfig.json instead if you prefer: https://www.typescriptlang.org/tsconfig#types

peitschie commented 2 months ago

Some related discussion at https://github.com/apache/cordova-plugin-file/issues/400#issuecomment-648731239

fudom commented 2 months ago

Curiously, adding it to types didn’t work in my repo. But the reference-comment works. Not my preferred solution, but it's fine since I only need it in one file. Thanks 👍