Skip to content

Commit 50832df

Browse files
OmbuwebNathanWalkerSeanKelly369
authored
docs: initial content for Using packages (#16)
Co-authored-by: Nathan Walker <[email protected]> Co-authored-by: Sean Kelly <[email protected]>
1 parent 8516f03 commit 50832df

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
title: Using packages (aka, plugins) in NativeScript
3+
---
4+
5+
### Plugins
6+
7+
NativeScript plugins are npm packages that often include additional native functionality. Consequently, discovering, installing, and removing NativeScript plugins resembles the process of working with npm packages used in Node.js or other front-end web development.
8+
9+
#### Finding plugins
10+
11+
The NativeScript team maintains a list of plugins [here](https://docs.nativescript.org/plugins/index.html).
12+
13+
Plugins can also be found at the [NativeScript marketplace](https://market.nativescript.org/) or on the [npm’s site](https://www.npmjs.com/)
14+
15+
Not finding the right plugin? You can try asking on our [Discord Community](https://nativescript.org/discord). Or perhaps [build a new plugin](/https://docs.nativescript.org/plugins/index.html#developing-your-own-plugins).
16+
<!-- TODO: fix links -->
17+
18+
You can also explore [@nativescript/core](https://docs.nativescript.org/understanding-packages.html#nativescript-core), which is included as a dependency in every NativeScript app. It is possible that the required functionality is already available.
19+
20+
#### Installing Plugins
21+
22+
Once the needed plugin is found, it can be installed in a NativeScript app using the `ns plugin add` command which behaves just like `npm install` while also modifying any existing platform projects (linking, etc.).
23+
24+
```cli
25+
ns plugin add <plugin-name>
26+
```
27+
28+
For example, the following command installs the [NativeScript camera plugin](https://docs.nativescript.org/plugins/camera.html).
29+
30+
```cli
31+
ns plugin add @nativescript/camera
32+
```
33+
34+
Instead of using plugin add, package managers like npm, yarn, or pnpm can also be used:
35+
36+
```cli
37+
npm install --save @nativescript/camera
38+
```
39+
40+
The installation of a NativeScript plugin is similar to installing a npm package. The NativeScript CLI downloads the plugin from npm and adds it to the node_modules folder in the project's root. This process also includes updating the root package.json file with the plugin and resolving its dependencies (if any).
41+
42+
#### Installing Plugins as Developer Dependencies
43+
44+
As demonstrated above, the command `ns plugin add @nativescript/camera` is equivalent to `npm i @nativescript/camera --save` internally. However, when installing a developer dependency, such as @nativescript/types or @nativescript/webpack, you need to explicitly save it as a devDependency. To do so, use the npm install command with the --save-dev flag. For instance:
45+
46+
```cli
47+
npm i @nativescript/types --save-dev
48+
```
49+
50+
::: tip Note
51+
Regarding package handling, **dependencies** are necessary for the application to run, while **devDependencies** are only needed during the development phase. For instance, the **@nativescript/camera** plugin is a dependency required at runtime to utilize the hardware camera. Conversely, **@nativescript/types** is a developer dependency used solely for IntelliSense during the development process. It is important to avoid installing `devDependencies` as `dependencies` to prevent large output build files and keep the application size manageable. You can find an example of a `package.json` file using both `dependencies` and `devDependencies` [here](https://github.com/NativeScript/nativescript-sdk-examples-js/blob/master/package.json#L31-L44).
52+
:::
53+
54+
#### Importing and Using Plugins
55+
56+
Once the required plugin is installed, it can be utilized in the project. It's crucial to carefully review the plugin's documentation and README file as each plugin may have specific configuration requirements. The following code snippet demonstrates the basic usage of the [@nativescript/camera](https://docs.nativescript.org/plugins/camera.html) plugin:
57+
58+
```javascript
59+
import { requestPermissions } from '@nativescript/camera'
60+
requestPermissions()
61+
```
62+
63+
```typescript
64+
import { requestPermissions } from '@nativescript/camera'
65+
requestPermissions()
66+
```
67+
68+
#### Removing Plugins
69+
70+
To remove a NativeScript plugin, you can use the following command.
71+
72+
```cli
73+
ns plugin remove <plugin-name>
74+
```
75+
76+
For example, the following command removes the NativeScript camera plugin.
77+
78+
```cli
79+
ns plugin remove @nativescript/camera
80+
```
81+
82+
As with installation, the removal of a NativeScript plugin mimics the removal of an npm package.
83+
84+
The NativeScript CLI removes plugin files from the app's node_modules folder in the project's root. It also removes the plugin's dependencies and removes the plugin from the project's root package.json file.
85+
86+
### Package Managers
87+
88+
A package manager is software that enables management of external code necessary for a project to function properly. The NativeScript CLI employs Node Package Manager (npm) as its default package manager. Upon creating a new app, the CLI automatically executes `npm install` to install all required dependencies.
89+
90+
#### Supported package managers
91+
92+
NativeScript CLI allows you to [configure the package manager](/project-structure/nativescript-config-ts#setting-project-package-manager) used when working with dependencies. When you change the defaultly used `npm` package manager, CLI will use the newly set package manager for all operations it executes related to project dependencies, for example, project creation, managing dependencies, etc.
93+
94+
NativeScript CLI supports three package managers:
95+
96+
- `npm` - this is the default option
97+
- `yarn` - you can set it by calling `ns package-manager set yarn`. More information about `yarn` is available [here](https://yarnpkg.com/)
98+
- `pnpm` - from version 6.4, you can use `pnpm` to manage the dependencies of your application. You can use `pnpm` by calling `ns package-manager set pnpm`. NOTE: You will have to use `--shamefully-hoist` flag if you call `pnpm` on your own. CLI passes this flag when installing dependencies with `pnpm` and probably your application will not work if you omit it. More information about `pnpm` is available [here](https://pnpm.js.org/).
99+
100+
In case you want to check what is the currently used package manager, you can use:
101+
102+
```cli
103+
ns package-manager get
104+
```

content/sidebar.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export default [
3434
text: 'Debugging',
3535
link: '/guide/debugging',
3636
},
37+
{
38+
text: 'Plugins',
39+
link: '/guide/development-workflow/using-packages'
40+
},
3741
],
3842
},
3943
{

0 commit comments

Comments
 (0)