Shopify app dev run startup code

Topic summary

A developer is experiencing issues with startup code execution when running the Shopify app template (Remix) using npm run dev or shopify app dev.

Core Problems:

  • Console.log statements in shopify.server.ts, entry.server.tsx, and other documented files don’t appear in stdout
  • Only /app/routes.ts seems to execute on server start
  • Environment variables from .env are undefined on first execution but load correctly after hot reload
  • Code appears to run once per hot reload rather than once per server start

Developer’s Goal:
Start a GCP Pub/Sub subscription listener when running npm run dev, but unclear how to execute arbitrary startup code.

Solution Provided:

The discussion remains open as the original poster plans to repost on the recommended community forum.

Summarized with AI on October 27. AI used: claude-sonnet-4-5-20250929.

Running the shopify app template remix
When running ‘npm run dev’ or ‘shopify app dev’ no code inside

How can I run arbitrary code on my node server using ‘shopify app dev’. Or is that somehow not intended or do I misunderstand the architecture completely?

Even better yet. The first time the code in route.ts is being executed, the .env variable has not been read, so any reads of process.env will result in an undefined. When hot reloading (saving any file, even without changes? CMD+S on mac) and re-executing the code the environment is loaded and all the variables show up as expected.
So this code is not run once per server start but somehow once per hot reload?

PS: What I really want to do is ‘npm run dev’ and then start my gcp pub/sub subscription, pulling/listening on messages. I have not read up on graphql and how that can help with this. Maybe that’s the way to go? Still confused about just calling some code.

Hello,

for an app development related question, I’d recommend posting on the dev community at https://community.shopify.dev/

As for understanding how a shopify app works, if you haven’t done it already I’d recommend that you follow the startup guide at https://shopify.dev/docs/apps/build/scaffold-app Is it what you’ve done?

Also, getting familiar with how remix works is a must (the file naming convention, the project structure, etc…), all the doc is at https://remix.run/docs/en/main/start/quickstart

For your specific question, you mentioned you want to start a worker process that connects to your pub/sub subscription, right? That can be achieved via a file shopify.web.toml. In that file you can tell shopify what to do when you run “shopify app dev”, so you’ll be able to start a background process from here.

I’d recommend taking a look at the related shopify’s documentation just here https://shopify.dev/docs/apps/build/cli-for-apps/app-structure#background-process as it will be explained much better than I can do it!

Thank you for the information, I will post it on https://community.shopify.dev/