Issue with setting up Next.js E-commerce example and resolving the error related to the port.
The error you're encountering is because the $PORT environment variable isn't set in your system. This is a common issue with the Next.js e-commerce example starter.
The Quick Fix:
You have a few options to resolve this:
Set the PORT environment variable before running the dev command:
PORT=3000 npm run devCreate a
.env.localfile in your project root and define the PORT:PORT=3000
Modify the package.json script to hardcode a port instead of using the variable:
"dev": "concurrently \"next --port 3000\" \"cd studio && sanity start\"",
Why This Happens:
The example assumes you'll have a PORT environment variable set, but it's not included in the starter template by default. The $PORT syntax is trying to reference this environment variable (on Unix-based systems; on Windows you'd use %PORT%).
Recommended Approach:
I'd suggest option 3 (hardcoding the port in package.json) for local development since it's the simplest and most reliable across different operating systems. Port 3000 is the standard Next.js development port. The Sanity Studio will run on its default port 3333.
If you need different ports for any reason, just change the number in the script. The important thing is that Next.js and Sanity Studio run on different ports so they don't conflict.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.