Skip to content

Commit f364da6

Browse files
fix: use require.resolve() for exec-child.js path to fix Next.js/webp… (#1259)
When Next.js (or any webpack-based bundler) bundles the server-side code, __dirname is replaced with the bundled output directory (e.g. .next/server/vendor-chunks/). This causes child_process.execFileSync to look for exec-child.js in the wrong location, leading to the 'Cannot find module' error. Fix: resolve exec-child.js path using require.resolve() at module load time. require.resolve() always returns the real filesystem path of the module, unaffected by bundler __dirname transformations.
1 parent ed9f79a commit f364da6

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/exec.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ var common = require('./common');
55
var _tempDir = require('./tempdir').tempDir;
66
var _pwd = require('./pwd');
77

8+
// Resolve exec-child.js path at module load time using require.resolve() so that
9+
// bundlers (e.g. Next.js/webpack) do not break the path by replacing __dirname
10+
// with the bundled output directory. require.resolve() always returns the real
11+
// filesystem path of the module, regardless of bundler transformations.
12+
var EXEC_CHILD_PATH = require.resolve('./exec-child.js');
13+
814
var DEFAULT_MAXBUFFER_SIZE = 20 * 1024 * 1024;
915
var DEFAULT_ERROR_CODE = 1;
1016

@@ -78,7 +84,7 @@ function execSync(cmd, opts, pipe) {
7884
writeFileLockedDown(paramsFile, JSON.stringify(paramsToSerialize));
7985

8086
var execArgs = [
81-
path.join(__dirname, 'exec-child.js'),
87+
EXEC_CHILD_PATH,
8288
paramsFile,
8389
];
8490

0 commit comments

Comments
 (0)