Skip to content

create-payload-app in existing Next.js project replaces .env and .env.example contents #10355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
smoores-dev opened this issue Jan 4, 2025 · 5 comments · Fixed by #10636
Closed
Assignees

Comments

@smoores-dev
Copy link

Describe the Bug

On the tin! I had a .env file with several variables in it; after running npx create-payload-app, the entire contents were replaced with:

# Added by Payload
DATABASE_URI=your-database-uri
PAYLOAD_SECRET=your-payload-secret
POSTGRES_URL=<postgres-url>

Link to the code that reproduces this issue

https://github.com/smoores-dev/payload-dotenv-clear-repro

Reproduction Steps

You can just see the issue by looking at the .env.example file between the two commits in that repo. To reproduce:

  1. git checkout init to check out the initial commit
  2. Add a .env file and put an environment variable in it
  3. npx create-payload-app (choose whatever options)
  4. Observe the replaced content in the .env.example file and .env file

Which area(s) are affected? (Select all that apply)

Not sure

Environment Info

Binaries:
  Node: 22.10.0
  npm: 10.9.0
  Yarn: 4.5.0
  pnpm: N/A
Relevant Packages:
  payload: 3.14.0
  next: 15.1.3
  @payloadcms/email-nodemailer: 3.14.0
  @payloadcms/graphql: 3.14.0
  @payloadcms/next/utilities: 3.14.0
  @payloadcms/payload-cloud: 3.14.0
  @payloadcms/richtext-lexical: 3.14.0
  @payloadcms/translations: 3.14.0
  @payloadcms/ui/shared: 3.14.0
  react: 19.0.0
  react-dom: 19.0.0
Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Dec 15 16:48:23 UTC 2024
  Available memory (MB): 64001
  Available CPU cores: 16
@smoores-dev smoores-dev added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction Auto-added tag on create to tell bot to check recreation URL, removed after check. labels Jan 4, 2025
@github-actions github-actions bot removed the validate-reproduction Auto-added tag on create to tell bot to check recreation URL, removed after check. label Jan 4, 2025
@jessrynkar jessrynkar self-assigned this Jan 14, 2025
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Jan 14, 2025
@jessrynkar
Copy link
Member

Screen.Recording.2025-01-15.at.4.02.49.PM.mov

Hi @smoores-dev, I used the reproduction you provided and am unable to replicate this behavior. Could you send a screenshot? There might be something I am missing in your steps.

@smoores-dev
Copy link
Author

Hi @JessChowdhury! It looks like you're running npx create-payload-app at the HEAD of the main branch of that repo? I'm seeing that the CLI says "update" Payload — the issue is when running the CLI in an app that doesn't yet have Payload installed. You can get to that state by running git checkout init, which will check out the first commit of the repo, from before Payload was installed.

@smoores-dev
Copy link
Author

┬─[smoores@smooreswork:~/c/payload-dotenv-clear-repro]─[11:25:59 AM]─[G:main]
╰─>$ git checkout init
Note: switching to 'init'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 91558dd Initial commit
┬─[smoores@smooreswork:~/c/payload-dotenv-clear-repro]─[11:26:05 AM]─[G:(init)]
╰─>$ npx create-payload-app
Need to install the following packages:
[email protected]
Ok to proceed? (y) 



┌   create-payload-app 
│
◇   ────────────────────────────────────────────╮
│                                               │
│  Welcome to Payload. Let's create a project!  │
│                                               │
├───────────────────────────────────────────────╯
│
◇   ▲ Next.js  project detected!
│
◇  Install Payload in this project?
│  Yes
│
◇  Select a database
│  SQLite
│
◇  Enter SQLite connection string
│  file:./payload-dotenv-clear-repro.db
│
◇  Successfully installed Payload and dependencies
│
◇  Payload project successfully initialized!
│
◇   Documentation  ───╮
│                     │
│  - Getting Started  │
│  - Configuration    │
│                     │
│                     │
├─────────────────────╯
│
└   Have feedback?  Visit us on GitHub.
┬─[smoores@smooreswork:~/c/payload-dotenv-clear-repro]─[11:26:37 AM]─[G:(init)]
╰─>$ git diff .env.example
diff --git a/.env.example b/.env.example
index 36410a4..8b7193d 100644
--- a/.env.example
+++ b/.env.example
@@ -1 +1,3 @@
-TEST_VAR=test
+# Added by Payload
+DATABASE_URI=your-connection-string-here
+PAYLOAD_SECRET=YOUR_SECRET_HERE

@jessrynkar
Copy link
Member

@smoores-dev thank you I see the issue now!

denolfe pushed a commit that referenced this issue May 1, 2025
### What?
Using `create-payload-app` to initialize Payload in an existing Next.js
app **that does not already have Payload installed** overwrites any
existing data in the `.env` and `.env.example` files.

The desired behavior is for Payload variables to get added with no
client data lost.

### How?
Updates `manageEnvFiles` to check for existing `.env / .env.example`
file and appends or creates as necessary.

Adds tests to
`packages/create-payload-app/src/lib/create-project.spec.ts`.

#### Fixes #10355
Copy link
Contributor

github-actions bot commented May 5, 2025

🚀 This is included in version v3.37.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants