diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md
index d1347ae5357..a41cca9283b 100644
--- a/docs/configuration/configuration.md
+++ b/docs/configuration/configuration.md
@@ -114,7 +114,7 @@ Env file `electron-builder.env` in the current dir ([example](https://github.com
---
* afterSign
(context: AfterPackContext) => Promise | null - The function (or path to file or module id) to be run after pack and sign (but before pack into distributable format).
-* beforeBuild
(context: BeforeBuildContext) => Promise | null - The function (or path to file or module id) to be run before dependencies are installed or rebuilt. Works when `npmRebuild` is set to `true`. Resolving to `false` will skip dependencies install or rebuild.
+* beforeBuild
(context: BeforeBuildContext) => Promise | null - The function (or path to file or module id) to be run before dependencies are installed or rebuilt. Works when `npmRebuild` is set to `true`. If provided and `node_modules` are missing it will not invoke production dependencies check. Resolving to `false` will skip dependencies install or rebuild.
* remoteBuild
= `true` Boolean - Whether to build using Electron Build Service if target not supported on current OS.
* includePdb
= `false` Boolean - Whether to include PDB files.
* removePackageScripts
= `true` Boolean - Whether to remove `scripts` field from `package.json` files.
diff --git a/packages/electron-builder-lib/src/configuration.ts b/packages/electron-builder-lib/src/configuration.ts
index c161a2ed1d3..74e209f0bf9 100644
--- a/packages/electron-builder-lib/src/configuration.ts
+++ b/packages/electron-builder-lib/src/configuration.ts
@@ -174,6 +174,8 @@ export interface Configuration extends PlatformSpecificBuildOptions {
/**
* The function (or path to file or module id) to be run before dependencies are installed or rebuilt. Works when `npmRebuild` is set to `true`. Resolving to `false` will skip dependencies install or rebuild.
+ *
+ * If provided and `node_modules` are missing, it will not invoke production dependencies check.
*/
readonly beforeBuild?: ((context: BeforeBuildContext) => Promise) | string| null
diff --git a/packages/electron-builder-lib/src/packager.ts b/packages/electron-builder-lib/src/packager.ts
index d22bd2d5a5e..670ee4220ee 100644
--- a/packages/electron-builder-lib/src/packager.ts
+++ b/packages/electron-builder-lib/src/packager.ts
@@ -8,6 +8,7 @@ import isCI from "is-ci"
import { dump } from "js-yaml"
import { Lazy } from "lazy-val"
import * as path from "path"
+import { exists } from "builder-util/out/fs"
import { AppInfo } from "./appInfo"
import { readAsarJson } from "./asar/asar"
import { AfterPackContext, Configuration } from "./configuration"
@@ -19,7 +20,7 @@ import { PlatformPackager, resolveFunction } from "./platformPackager"
import { computeArchToTargetNamesMap, createTargets, NoOpTarget } from "./targets/targetFactory"
import { computeDefaultAppDirectory, getConfig, validateConfig } from "./util/config"
import { computeElectronVersion, getElectronVersionFromInstalled } from "./util/electronVersion"
-import { createLazyProductionDeps, Dependency } from "./util/packageDependencies"
+import { Dependency, getProductionDependencies } from "./util/packageDependencies"
import { checkMetadata, readPackageJson } from "./util/packageMetadata"
import { getRepositoryInfo } from "./util/repositoryInfo"
import { getGypEnv, installOrRebuild } from "./util/yarn"
@@ -91,7 +92,15 @@ export class Packager {
get productionDeps(): Lazy> {
let result = this._productionDeps
if (result == null) {
- result = createLazyProductionDeps(this.appDir)
+ // https://github.com/electron-userland/electron-builder/issues/2551
+ result = new Lazy(async () => {
+ if (this.config.beforeBuild == null || (await exists(path.join(this.appDir, "node_modules")))) {
+ return await getProductionDependencies(this.appDir)
+ }
+ else {
+ return []
+ }
+ })
this._productionDeps = result
}
return result
diff --git a/test/out/__snapshots__/filesTest.js.snap b/test/out/__snapshots__/filesTest.js.snap
index 6847aeaab7f..95b9fc35417 100644
--- a/test/out/__snapshots__/filesTest.js.snap
+++ b/test/out/__snapshots__/filesTest.js.snap
@@ -91,8 +91,8 @@ Array [
"lib/net45/node.dll",
"lib/net45/pdf_viewer_resources.pak",
"lib/net45/snapshot_blob.bin",
- "lib/net45/Test%20App%20%C3%9FW.exe",
- "lib/net45/Test%20App%20%C3%9FW_ExecutionStub.exe",
+ "lib/net45/Test App ßW.exe",
+ "lib/net45/Test App ßW_ExecutionStub.exe",
"lib/net45/ucrtbase.dll",
"lib/net45/ui_resources_200_percent.pak",
"lib/net45/Update.exe",