Next.JS
This document provides instructions for working with Next.js and the CommandK CLI. It explains how to run your Next.js application and manage secrets effectively using the CommandK CLI.
Prerequisites
Before you begin the integration process, make sure you have the following:
- API Access Token: To authenticate your Next.js application with CommandK, you'll need an API access token. If you haven't obtained one yet, refer to this link for instructions on how to acquire it.
Integration Steps
Follow these steps to integrate CommandK with your Next.js application:
Step 1: Create a Next.js Application
If you haven't already created a Next.js application, you can use the create-next-app
command to set up a new project:
npx create-next-app@latest --use-npm my-nextjs-app
cd my-nextjs-app
Step 2: Access Secrets in Your Next.js Application
Inside your Next.js application, you can access secrets as environment variables. Open the pages/_app.js
file and add the following code snippet:
import '@/styles/globals.css'
export default function App({ Component, pageProps }) {
console.log('Hello, ', process.env.NEXT_PUBLIC_NAME);
return <Component {...pageProps} />
}
This code will log the value of the NEXT_PUBLIC_NAME
environment variable to the console, allowing you to verify that secrets are accessible.
In a real-world scenario, you should avoid logging secrets or any sensitive information. The code snippet provided is for demonstration purposes only and should not be used in production or any security-critical environments.
Running Your Next.js Application
To run your Next.js application with the CommandK CLI, you can use the following command:
$ cmdk run <application-name> --environment development -- npm run dev
Replace <application-name>
with the actual name of your Next.js application. This command will execute your Next.js application in the development environment.
Running with dotenv
If your Next.js application uses dotenv
to manage environment variables, you can utilize the CommandK CLI to write these variables to an .env
file and then run your service. Follow these steps:
- Execute the following command:
$ cmdk run <application-name> --environment development \
--run-type file-store \
--file-name .env.local \
--file-format env \
-- npm run dev
This command will write your environment variables to the .env.local
file in the specified env
format and then run your Next.js service.
Adjust the file name and format to match your project's configuration if necessary.
By following these instructions, you can seamlessly integrate the CommandK CLI with your Next.js application, making it easy to manage secrets and run your application securely.
Updated 12 months ago