NativeScript - Plugins



The npm package is used for adding native functionality. Using this package, we can install or search or delete any plugins. This section explains about plugins in detail.

Commands

add − It is used to install plugin.

update − Updates specified plugin and modify its dependencies.

remove − Removes the plugin.

build − It is used to build plugin for iOS or android projects.

create − Creates a plugin for your project.

Adding Plugin

Below syntax is used to add a new plugin −

tns plugin add <plugin-name>

For example, if you want to add nativescript-barcodescanner, you can use the following code −

tns plugin add nativescript-barcodescanner

You could see the following response −

+ nativescript-barcodescanner@3.4.1 
added 1 package from 1 contributor and audited 11704 packages in 8.76s

You can also use npm module to add the above plugin −

npm install nativescript-barcodescanner

Now, NativeScript CLI downloads the plugin from npm and add inside your node_modules folder.

If you want to add the plugin directly to your package.json and resolve all the dependency issues, you can use the below command instead of the previous one −

npm i nativescript-barcodescanner

If you want to install a developer dependencies during development, use the below code −

npm i tns-platform-declarations --save-dev

Here,

tns-platform-declarations is a developer dependency required only for intelliSense during the development process.

Importing Plugins

Now, we have installed nativescript-barcodescanner plugin. Let us add inside your project using the below command −

const maps = require("nativescript-barcodescanner"); 
maps.requestPermissions();

Updating Plugins

This method is used to update a specified plugin so it uninstalls previous one and installs new version and modify its dependencies. It is defined below −

tns plugin update <Plugin name version>

Removing Plugin

If you want remove the plugin, if not required, you can use the below syntax −

tns plugin remove <plugin-name>

For example, if you want to remove the above installed nativescript-google-maps-sdk, then use the below command −

tns plugin remove nativescript-barcodescanner

You could see the following response −

Successfully removed plugin nativescript-barcodescanner

Building Plugins

It is used to build the plugin’s Android-specific project files located in platforms/android. Let us build the nativescript-barcodescanner pugin using the below command −

tns plugin build nativescript-barcodescanner

Creating Plugins

NativeScript plugins are simple JavaScript modules. It is defined inside your application src\package.json file. This module is used to create a new project for NativeScript plugin development. It is defined below −

tns plugin create <Plugin Repository Name> [--path <Directory>]
Advertisements