Skip to content

Master the 10-Minute, 10-Step Guide: Converting Your Next.js App to Mobile with Capacitor

Published July 14, 2023
nextjs
capacitor
mobile

A step-by-step guide to transform your Next.js application into a cross-platform mobile application using Capacitor.

Capacitor, a cross-platform app runtime, enables web apps to run on Android, iOS, and the web. For web developers looking to extend their applications onto mobile platforms, Capacitor is a fantastic tool to achieve this transition. Today, we'll look at how to transform a Next.js application into a mobile application using Capacitor.

Step 1: Prepare Your Next.js Application

To begin, ensure that your Next.js application is ready for conversion. Check that your application is fully functional and debugged. Don't forget to test all the features, ensuring that everything works as expected.

Step 2: Install Capacitor

Once your Next.js application is ready, it's time to install Capacitor. To install Capacitor, open the terminal, navigate to your project directory and run the following command:

bash
npm install @capacitor/cli @capacitor/core

This will install both Capacitor's command-line interface and core files.

Step 3: Initialize Capacitor

After the installation is complete, initialize Capacitor with your app information:

bash
npx cap init

You'll be prompted to enter the app's name and an app ID (usually in domain format, but it doesn't have to be a valid domain).

Step4: Update Next.js config.

Open next.config.js and update following

java
const nextConfig = {
  output: 'export',
}

module.exports = nextConfig

Step 5: Build Your Next.js Application

Build your Next.js application for production by running the command:

bash
npm run build

This command tells Next.js to prepare your application for deployment. It creates a 'out' folder with all the required static files and will also create ‘.next’ folder, which includes the optimized version of your app.

Step 6: Configure Capacitor

Capacitor considers the 'public' folder as the web directory by default. However, Next.js generates the output files into the '.next' folder and for our use case ‘out’ folder that holds all the static files. To change the web directory, open the 'capacitor.config.json' file and change the 'webDir' from 'public' to 'out'. The updated config file will look like this:

json
{
  "appId": "com.example.app",
  "appName": "myApp",
  "webDir": "out",
  "server": {
    "androidScheme": "https"
  }
}

Step 7: Add Platforms

Next, you need to add the platforms you want your app to run on. For Android and iOS, use the following commands:

bash
npx cap add android
npx cap add ios

These commands create 'android' and 'ios' folders at the root of your project.

Step 8: Sync app changes

Since you generated output static file that will be used by Capacitor. Run following command.

bash
npx cap sync

This command will copy files from out directory and paste it inside android and ios directory. It will also install/update any plugins.

Step 9: Build Your App

Finally, open your platform-specific IDE (Android Studio for Android, Xcode for iOS), and build your app from there. You'll find your app in the 'android' or 'ios' directories. Open these directories in the respective IDEs, and build your app for testing or distribution. You can also use following commands for same

bash
npx cap open ios
npx cap open android

And voila! Your Next.js application is now a mobile application. This is just the tip of the iceberg for Capacitor. There's so much more that you can do, including adding native functionalities. But that's a topic for another day. Until then, happy coding!


Stay connected! Follow me on Twitter and YouTube for more tech insights.