Complete Setup Guide

App Setup

Installing Capacitor

There are two ways to create your Capacitor application. You can use the @capacitor/create-app package to create a Capacitor application from scratch, or you can add Capacitor to your already existing web project.

Remember to make sure your environment is set up for the platforms you will be building for.

Creating a new Capacitor application

The @capacitor/create-app package can be used to quickly create a Capacitor application. You can run the following command in an empty directory to scaffold a new Capacitor application.

npm init @capacitor/app

Copy

Adding Capacitor to your existing web application

Capacitor was designed to drop into any modern JavaScript web app. However, your project needs to have the following three requirements in order to use Capacitor with your existing application.

Your project must have...

  • A package.json file

  • A separate directory for built web assets such as dist or www

  • An index.html file at the root of your web assets directory

Info

Your index.html file must have a <head> tag in order to properly inject Capacitor. If you do not have a <head> in your Html, Capacitor plugins will not work.

Install Capacitor

In the root of your app, install Capacitor's main npm depdencies: the core JavaScript runtime and the command line interface (CLI).

npm i @capacitor/core
npm i -D @capacitor/cli

Copy

Initialize your Capacitor config

Then, initialize Capacitor using the CLI questionnaire:

npx cap init

Copy

The CLI will ask you a few questions, starting with your app name, and the package ID you would like to use for your app.

Create your Android and iOS projects

After the Capacitor core runtime is installed, you can install the Android and iOS platforms.

npm i @capacitor/android @capacitor/ios

Copy

Once the platforms have been added to your package.json, you can run the following commands to create your Android and iOS projects for your native application.

npx cap add android
npx cap add ios

Copy

Sync your web code to your native project

Once you've created your native projects, you can sync your web application to your native project by running the following command.

npx cap sync

Copy

npx cap sync will copy your built web application, by default www, to your native project and install the native projects dependencies.

Last updated