fix(desktop): launch normally when NODE_ENV=development is set (#5053) by Jocs · Pull Request #5374 · marktext/marktext · GitHub
Skip to content

fix(desktop): launch normally when NODE_ENV=development is set (#5053) - #5374

Merged
Jocs merged 3 commits into
developfrom
fix/5053-packaged-app-node-env
Sep 16, 2026
Merged

Jocs merged 3 commits into
developfrom
fix/5053-packaged-app-node-env

Conversation

@Jocs

@Jocs Jocs commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Closes #5053
Closes #5083

Summary

Both reports show MarkText 0.19.1 on Windows crashing on every launch with:

TypeError: Invalid URL
    at new URL (node:internal/url:819:25)
    at EditorWindow._buildUrlWithSettings (…\app.asar\out\main\index.js:5816:17)

The dialog and the issue title also show raw i18n keys (error.unexpectedMainProcess, error.unexpectedErrorWithMessage).

Cause. Both happen when the user's environment has NODE_ENV=development, which is a common global setting on Node.js developers' machines. The main process checks process.env.NODE_ENV at runtime to tell electron-vite dev from a built app. electron-vite leaves process.env untouched in the main bundle, so the user's value reaches a packaged app, and the app takes every development branch:

  • _buildUrlWithSettings loads the window from process.env.ELECTRON_RENDERER_URL. Only electron-vite dev sets that variable, so new URL(undefined) throws. This is the crash.
  • loadTranslations looks for locales under the working directory. It finds nothing, so every main-process string falls back to its key.
  • cli() drops the command line (files to open, --user-data-dir) and uses the marktext-dev profile instead.
  • The single instance lock is skipped, and the log levels change.

Fix. These checks now read import.meta.env.DEV, which electron-vite replaces at build time: true under electron-vite dev, false in electron-vite build. In the built main bundle the development branches are removed. baseUrl is always the file:// URL, the lock is always requested, and ELECTRON_RENDERER_URL no longer appears.

Changed checks: windows/base.ts (renderer URL), cli/index.ts (dev profile), main/index.ts (single instance lock, console log level), utils/index.ts (file log level), common/i18n.ts (locale directory), and common/filesystem/paths.ts (getResourcesPath, currently unused).

Until a release with this fix ships, affected users can remove NODE_ENV from their environment (or start MarkText from a shell where it is unset).

Reproduction

On macOS with the packaged 0.19.1 app, NODE_ENV=development marktext produces the same stack with the same line and column numbers as both reports, plus 308 Translation file not found errors. 0.20.0-rc.2 and a develop build crash the same way (_buildUrlWithSettings → Invalid URL).

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added
    • issue-5053-node-env-development.spec.ts (e2e, built app) launches with NODE_ENV=development. It checks that the window loads from file://, that the --user-data-dir from the command line is used, and that the app holds the single instance lock. It fails on develop: no window opens, and the main log shows Invalid URL at _buildUrlWithSettings. It passes with this change.
    • launchElectron gets an env option so a spec can set environment variables for the app.
  • electron-vite dev still runs in development mode. The window loads http://localhost:5173/?udp=…marktext-dev…, and info logs are printed.
  • pnpm run typecheck passes, eslint reports 0 errors on the changed files, and all desktop unit tests pass (818/818).
  • Tested on: macOS (built app via Playwright, packaged 0.19.1 / rc.2 for the reproduction)

Notes for reviewers

env.ts still reads NODE_ENV !== 'production' for isDevMode and debug. Those checks give the same result whether NODE_ENV is unset or development, so they don't cause this crash, and I left them alone. Changing them would turn debug mode off in every packaged build, which is a separate decision.

#5375 (#3020) changes the single instance lock call on the line after the if changed here in packages/desktop/src/main/index.ts. Whichever PR merges second needs a trivial rebase.

🤖 Generated with Claude Code


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Jocs and others added 3 commits September 15, 2026 12:17
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The main process read process.env.NODE_ENV at runtime to tell a dev run
from a built app, so a NODE_ENV=development exported by the user's
environment switched a built app into development mode: it replaced the
command line with the marktext-dev profile, skipped the single instance
lock, looked for locales in the working directory, and loaded the window
from ELECTRON_RENDERER_URL, which only electron-vite dev sets. The last
one crashed every launch with "TypeError: Invalid URL".

Check import.meta.env.DEV instead, which electron-vite fixes at build
time.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

@Jocs
Jocs merged commit a95dd51 into develop Sep 16, 2026
10 checks passed
@Jocs
Jocs deleted the fix/5053-packaged-app-node-env branch September 16, 2026 03:30
Jocs added a commit that referenced this pull request Sep 16, 2026
`debug` and `isDevMode` were derived from `NODE_ENV !== 'production'`, and
nothing sets NODE_ENV in a packaged app, so every release ran as a debug
build: the View menu offered "Show Developer Tools" and "Reload Window", the
settings window bound the developer-tools shortcut, Preferences showed the
keyboard debugging tools, and the command manager verified its default
commands on every start.

Both flags now read `import.meta.env.DEV`, which electron-vite replaces at
build time — the move #5374 made for the other development checks, which
left this one alone because it behaves the same whether NODE_ENV is unset or
`development`. `--debug` and MARKTEXT_DEBUG still turn debug mode on.

A new e2e spec launches the built app twice, plainly and with
MARKTEXT_DEBUG=1, and checks that only the second one carries the three extra
View menu entries.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
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 this pull request may close these issues.

error.unexpectedErrorWithMessage error.unexpectedErrorWithMessage

1 participant