Repository navigation
fix(create-electron-app): make import safe on ESM projects and existing Forge configs - #4467
Merged
Merged
Conversation
…ng Forge configs
`create-electron-app import` was still the Forge 6 script and broke on
common 2026 project shapes:
- A project with `"type": "module"` got the base template's CommonJS
`forge.config.js`, which Node then refuses to load (`require is not
defined in ES module scope`). The template config is now rewritten as
an ES module for those projects.
- An existing `forge.config.{js,mjs,cjs,ts,mts,cts}` file was not
detected (only `config.forge.makers` in package.json was), so import
overwrote it. All config locations Forge loads are now detected and
kept as-is.
- When `config.forge` was an object in package.json, import merged the
template config into the imported module namespace and then wrote the
unchanged package.json back, discarding the result. The existing
config is now kept untouched instead.
- The package manager was resolved from `process.cwd()`, not from the
project directory, so `import ../other-app` picked the wrong one.
`resolvePackageManager` gains a `cwd` option, import passes the
project directory, and a `--package-manager` flag matches `init`.
- `.yarnrc.yml` was written for Yarn Classic too; it is now only written
for Yarn Berry, and pnpm projects get the template's
`pnpm-workspace.yaml` so Electron's build script is allowed.
- The `release` script was not added, unlike the base template.
The docs still pointed at `npx electron-forge import`, which was removed
from the CLI in #4148; they now use `npx create-electron-app@latest
import`.
`isYarnBerry` moves from template-base to core-utils so import can share
it, and `lodash-es` is no longer needed by create-electron-app.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG
…project directory The Windows fast-tests job failed because the new import spec resolved a pnpm lockfile in a temp project and `resolvePackageManager` then ran `pnpm --version` in the current directory, where the Corepack shim refused with "This project is configured to use yarn". Run the version probe with the `cwd` option so a Corepack shim sees the imported project's `packageManager` field, and mock `spawn` in the import spec so the test never runs a package manager for real. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG
- Keep an existing `release` script; only add Forge's when none is set. - Treat a string `config.forge` as an existing config only when the file it points to exists, mirroring core's `forgeConfigIsValidFilePath`. - Prefer the lockfile in the project directory over the user agent of the package manager running the command (`npx` always reports npm), via a new `preferLockfile` option on `resolvePackageManager`. - Require a value for `--package-manager` so a bare flag is rejected. - Drop the `pnpm-workspace.yaml` copy: it would nest a workspace file inside a pnpm monorepo and its `allowBuilds` key is not understood by older pnpm 10 releases. pnpm setup for imports is deferred. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG
…gent With `preferLockfile`, `findUp` could return a stray lockfile from any ancestor directory (e.g. `~/yarn.lock`) and switch the install to a package manager the project never used. Only count a lockfile that sits in `cwd` or beside a `package.json` in an ancestor, such as a workspace root. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG
On Windows `path.resolve('/some/project')` gains a drive letter and
backslashes, so a mocked POSIX lockfile path never matched `cwd`.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG
georgexu99
approved these changes
Nov 3, 2026
georgexu99
left a comment
Contributor
There was a problem hiding this comment.
Stamped- the import path is in much better shape. A few nits inline, none blocking.
🤖 - a message from George's lil' guy
- Match core's config precedence in `findExistingForgeConfig`: object, then `forge.config.*` files, then a `config.forge` path string. - Throw if CommonJS syntax survives the ESM conversion of the template config, so template drift fails loudly. - Probe the project's Yarn version when `--package-manager yarn` is given without a version, so Yarn Classic projects do not get a `.yarnrc.yml`. - Explain the failure when a lockfile's package manager cannot be run, instead of a bare spawn ENOENT. - Restore an unset `npm_config_user_agent` properly in the import spec. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG
georgexu99
approved these changes
Nov 4, 2026
georgexu99
left a comment
Contributor
There was a problem hiding this comment.
Re-stamped on f69c7db- all five addressed, the loud throw on template drift is exactly what i wanted.
🤖 - a message from George's lil' guy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by Erick · project thread
Summarize your changes:
create-electron-app importis still essentially the Forge 6 script and breaks on common project shapes. This is the first, correctness-only slice of a larger modernization; it changes no defaults for projects that worked before.Bugs fixed
"type": "module"received the base template's CommonJSforge.config.js, and sinceforge-config.tsloads.jswith nativeimport(), every Forge command then failed withrequire is not defined in ES module scope. The template config is now rewritten as an ES module (import … from/export default) for those projects. The test loads the generated file through Node to prove it parses as ESM.forge.config.*files were overwritten. Onlyconfig.forge.makersin package.json counted as "existing config", so re-running import on a Forge project replacedforge.config.js, and a.ts/.mjsconfig got shadowed by a new.jsone. All six extensions Forge loads are now detected and the existing config is kept. A stringconfig.forgecounts only when the file it points to exists, mirroring core'sforgeConfigIsValidFilePath.config.forgemerge discarded its result. Itimport()ed the template,lodash.merged the user's config into the module namespace object, then re-read and re-wrote the unchanged package.json. The existing config is now simply kept untouched (and thelodash-esdependency goes away).npxalways won.resolvePackageManager()searched for a lockfile fromprocess.cwd(), and thenpm_config_user_agentset bynpxtook precedence over any lockfile, sonpx create-electron-app importinstalled into a pnpm or Yarn project with npm.resolvePackageManagergains{ cwd, preferLockfile }options; import passes the project directory and prefers its lockfile, and the<pm> --versionprobe runs in that directory so a Corepack shim sees the project'spackageManagerfield. Import also gets a--package-manager <name>flag likeinit..yarnrc.ymlwithnodeLinker: node-moduleswas written for Yarn Classic too; it is now only written for Yarn Berry. pnpm projects are left alone, as before.releasescript is added when the project has none, matching the base template. An existingreleasescript is kept.Docs
docs/cli.mdanddocs/import-existing-project.mdstill saidnpx electron-forge import, which was removed from the CLI in #4148. They now usenpx create-electron-app@latest importand document the two flags.Refactor
isYarnBerrymoves fromtemplate-basetocore-utilssoimportcan share it (andtemplate-basedrops its directsemverdependency).lodash-esand@types/lodash-esare removed from the lockfile since nothing uses them anymore.Tests
New fast spec
packages/external/create-electron-app/spec/fast/import.spec.ts(install and package manager spawns mocked, real temp dirs) covering the CJS and ESM config output, each existing-config location,config.forgepreservation, lockfile-in-dirdetection and precedence over the user agent, the keptreleasescript, and the Yarn Classic/Berry.yarnrc.ymlbehaviour.resolvePackageManager's new options get cases in the core-utils spec.Verified locally:
tsc -b packages,yarn test:fast(731 passed),lint:js,lint:markdown,lint:markdown-links,knip.Not in this PR (planned follow-ups): wiring the confirmation prompts, which have never been connected in either Forge 7 or 8; pnpm setup for imported projects (
allowBuilds/onlyBuiltDependencies, version-gated); a--dry-run; and an electron-builder config migrator.🤖 Generated with Claude Code
https://claude.ai/code/session_01KsmQ6ffJyGGUpoBmNeUuvG