Hi, I recently started using Capacitor.
I’m working on a single-page app built with React, Next.js, and Capacitor, and I’m trying to run it on Android.
I’ve successfully embedded the HTML/CSS/JS and made API requests to localhost:3000.
Now I want to prepare for production releases with continuous updates, so I plan to switch the API host based on the environment using code like this:
const host = process.env.NEXT_PUBLIC_API_HOST;
const request = async (path: string) => {
const url = `${host}/${path}`;
const response: HttpResponse = await CapacitorHttp.get({
url: url,
});
if (response.status !== 200) throw new Error("failed");
return await response.data;
};
The issue is that next build always uses NODE_ENV=production, so I can’t test the Android build with .env.development.
How can I test with a different API host (like development) on an Android device, without modifying the source code each time?
Any advice would be appreciated!
# package.json
"scripts": {
"android:build": "rm -rf out_capacitor && npm run build && cp -r out out_capacitor && rm -rf out_capacitor/api && npx cap sync",
"reverse": "adb -s or8hifdyu4zp9hkv reverse tcp:3000 tcp:3000",
"devDependencies": {
"@capacitor/cli": "^7.4.1",
"dependencies": {
"@capacitor/android": "^7.4.1",
"@capacitor/core": "^7.4.1",
"@capacitor/status-bar": "^7.0.1",
"next": "^15.3.1",
# next.config.ts
const isDev = process.env.NODE_ENV !== "production";
/** @type {import('next').NextConfig} */
module.exports = {
output: "export",
distDir: isDev ? "out_dev" : "out",
trailingSlash: true,
};
# capacitor.config.ts
import type { CapacitorConfig } from "@capacitor/cli";
const config: CapacitorConfig = {
appId: "com.toramilab.investool",
appName: "investool",
webDir: "./out_capacitor",
plugins: {
CapacitorHttp: {
enabled: true,
},
},
};
export default config;