One Dev Script, Two Operating Systems

By Next Feline ·

I work on the same project from a Windows machine and a Chromebook, and I wanted the dev server on a fixed port, 2026, and reachable from other devices on my network. That sounded like one line in package.json. It turned out to be two, because Windows and Linux set environment variables differently:

"dev Win": "set PORT=2026 && set browser=NONE&& next dev -H 0.0.0.0",
"dev ChromeOS": "PORT=2026 browser=NONE next dev -H 0.0.0.0",

On Linux, including ChromeOS's Linux environment, you can put variables in front of a command. On Windows, set is a separate command, chained with &&.

Notice there is no space before && in set browser=NONE&&. That is deliberate. In Windows, set PORT=2026 && ... includes the space before &&, so the value becomes "2026 ", with a trailing space. It is an easy way to create a bug that is very hard to see.

Then I discovered that I did not need the environment variables at all. Next.js accepts the port as a flag:

"dev": "next dev -p 2026 -H 0.0.0.0"

That single script works everywhere, with no Windows or Linux differences. -p sets the port and -H 0.0.0.0 makes the server listen on all network interfaces, which is what lets my phone, or the Chromebook's Linux container, reach it.

As for browser=NONE, that comes from Create React App, which opens a browser tab on start. Next.js does not do that, so the variable was never doing anything.

If you ever need environment variables that work on every system, the cross-env package solves it. But reaching for a built-in flag first is usually simpler.