@@ -128,6 +128,7 @@ export async function createVendureApp(
128128
129129 const portSpinner = spinner ( ) ;
130130 let port : number ;
131+ let storefrontPort : number = STOREFRONT_PORT ;
131132 portSpinner . start ( `Establishing port...` ) ;
132133 try {
133134 port = await findAvailablePort ( SERVER_PORT , PORT_SCAN_RANGE ) ;
@@ -175,7 +176,23 @@ export async function createVendureApp(
175176 // Determine the server root directory (either root or apps/server for monorepo)
176177 const serverRoot = includeStorefront ? path . join ( root , 'apps' , 'server' ) : root ;
177178 const storefrontRoot = path . join ( root , 'apps' , 'storefront' ) ;
178- const storefrontPort = STOREFRONT_PORT ;
179+
180+ // Find an available storefront port if including storefront
181+ if ( includeStorefront ) {
182+ const storefrontPortSpinner = spinner ( ) ;
183+ storefrontPortSpinner . start ( `Establishing storefront port...` ) ;
184+ try {
185+ // Start scanning from the higher of STOREFRONT_PORT or serverPort + 1
186+ // to avoid conflicts with the server port
187+ const storefrontStartPort = Math . max ( STOREFRONT_PORT , port + 1 ) ;
188+ storefrontPort = await findAvailablePort ( storefrontStartPort , PORT_SCAN_RANGE ) ;
189+ storefrontPortSpinner . stop ( `Using storefront port ${ storefrontPort } ` ) ;
190+ } catch ( e : any ) {
191+ storefrontPortSpinner . stop ( pc . red ( 'Could not find an available storefront port' ) ) ;
192+ outro ( e . message ) ;
193+ process . exit ( 1 ) ;
194+ }
195+ }
179196
180197 process . chdir ( root ) ;
181198 if ( packageManager !== 'npm' && ! checkThatNpmCanReadCwd ( ) ) {
@@ -250,10 +267,13 @@ export async function createVendureApp(
250267 storefrontSpinner . start ( `Downloading Next.js storefront...` ) ;
251268 try {
252269 await downloadAndExtractStorefront ( storefrontRoot ) ;
253- // Update storefront package.json name
270+ // Update storefront package.json name and dev script port
254271 const storefrontPackageJsonPath = path . join ( storefrontRoot , 'package.json' ) ;
255272 const storefrontPackageJson = await fs . readJson ( storefrontPackageJsonPath ) ;
256273 storefrontPackageJson . name = 'storefront' ;
274+ if ( storefrontPackageJson . scripts ?. dev ) {
275+ storefrontPackageJson . scripts . dev = `next dev --port ${ storefrontPort } ` ;
276+ }
257277 await fs . writeJson ( storefrontPackageJsonPath , storefrontPackageJson , { spaces : 2 } ) ;
258278
259279 // Generate storefront .env.local from template
0 commit comments