dottodot
dottodot

Reputation: 1619

Cannot find module after creating and adding plugin in Nativescript

Using the latest version of Nativescript I've created a plugin as per the documentation and after running tns plugin add ../nativescript-keychain I get the message Successfully installed plugin nativescript-keychain.

I can also see that it's been added to the node_modules directory of my app but require("nativescript-keychain") doesn't work as I get the error Cannot find module 'nativescript-keychain'

My plugin package.json looks like

{
  "name": "nativescript-keychain",
  "version": "0.0.1",
  "nativescript": {
    "platforms": {
      "ios": "2.2.1"
    }
  }
}

Upvotes: 4

Views: 3038

Answers (1)

Nathanael
Nathanael

Reputation: 5399

There are several reasons why this might occur; it would be helpful if you provided a repo to see all the code.

  1. package.json doesn't have a link to the source, typically you have a main: "somefile" key.
  2. Did you do tns run ios --emulator after you installed the plugin, you have to rebuild the app before it will take effect, plugins can't be synced via livesync...
  3. Is the code TypeScript or JavaScript, if it is TypeScript it needs to be transpiled to JS before you can add it to your demo application. TNS will NOT compile any TS code in the plugins. Plugins have to ship with the final JS code.
  4. You need typings for TS to use the auto-complete and not throw warnings about what methods are available.

Upvotes: 2

Related Questions