Skip to content

Commit f84de90

Browse files
committedSep 22, 2022
use a custom message for macOS architecture issues
1 parent ba91ed1 commit f84de90

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed
 

‎lib/npm/node-platform.ts

+40-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ declare const ESBUILD_VERSION: string;
99
// external code relies on this.
1010
export var ESBUILD_BINARY_PATH: string | undefined = process.env.ESBUILD_BINARY_PATH || ESBUILD_BINARY_PATH;
1111

12+
const packageDarwin_arm64 = 'esbuild-darwin-arm64'
13+
const packageDarwin_x64 = 'esbuild-darwin-64'
14+
1215
export const knownWindowsPackages: Record<string, string> = {
1316
'win32 arm64 LE': 'esbuild-windows-arm64',
1417
'win32 ia32 LE': 'esbuild-windows-32',
@@ -132,11 +135,7 @@ export function generateBinPath(): { binPath: string, isWASM: boolean } {
132135
// helpful in that case.
133136
const otherPkg = pkgForSomeOtherPlatform();
134137
if (otherPkg) {
135-
throw new Error(`
136-
You installed esbuild on another platform than the one you're currently using.
137-
This won't work because esbuild is written with native code and needs to
138-
install a platform-specific binary executable.
139-
138+
let suggestions = `
140139
Specifically the "${otherPkg}" package is present but this platform
141140
needs the "${pkg}" package instead. People often get into this
142141
situation by installing esbuild on Windows or macOS and copying "node_modules"
@@ -146,14 +145,46 @@ Windows and WSL environments.
146145
If you are installing with npm, you can try not copying the "node_modules"
147146
directory when you copy the files over, and running "npm ci" or "npm install"
148147
on the destination platform after the copy. Or you could consider using yarn
149-
instead which has built-in support for installing a package on multiple
148+
instead of npm which has built-in support for installing a package on multiple
150149
platforms simultaneously.
151150
152151
If you are installing with yarn, you can try listing both this platform and the
153152
other platform in your ".yarnrc.yml" file using the "supportedArchitectures"
154153
feature: https://yarnpkg.com/configuration/yarnrc/#supportedArchitectures
155154
Keep in mind that this means multiple copies of esbuild will be present.
155+
`
156+
157+
// Use a custom message for macOS-specific architecture issues
158+
if (
159+
(pkg === packageDarwin_x64 && otherPkg === packageDarwin_arm64) ||
160+
(pkg === packageDarwin_arm64 && otherPkg === packageDarwin_x64)
161+
) {
162+
suggestions = `
163+
Specifically the "${otherPkg}" package is present but this platform
164+
needs the "${pkg}" package instead. People often get into this
165+
situation by installing esbuild with npm running inside of Rosetta 2 and then
166+
trying to use it with node running outside of Rosetta 2, or vice versa (Rosetta
167+
2 is Apple's on-the-fly x86_64-to-arm64 translation service).
168+
169+
If you are installing with npm, you can try ensuring that both npm and node are
170+
not running under Rosetta 2 and then reinstalling esbuild. This likely involves
171+
changing how you installed npm and/or node. For example, installing node with
172+
the universal installer here should work: https://nodejs.org/en/download/. Or
173+
you could consider using yarn instead of npm which has built-in support for
174+
installing a package on multiple platforms simultaneously.
175+
176+
If you are installing with yarn, you can try listing both "arm64" and "x64"
177+
in your ".yarnrc.yml" file using the "supportedArchitectures" feature:
178+
https://yarnpkg.com/configuration/yarnrc/#supportedArchitectures
179+
Keep in mind that this means multiple copies of esbuild will be present.
180+
`
181+
}
156182

183+
throw new Error(`
184+
You installed esbuild for another platform than the one you're currently using.
185+
This won't work because esbuild is written with native code and needs to
186+
install a platform-specific binary executable.
187+
${suggestions}
157188
Another alternative is to use the "esbuild-wasm" package instead, which works
158189
the same way on all platforms. But it comes with a heavy performance cost and
159190
can sometimes be 10x slower than the "esbuild" package, so you may also not
@@ -171,8 +202,9 @@ want to do that.
171202
throw new Error(`The package "${pkg}" could not be found, and is needed by esbuild.
172203
173204
If you are installing esbuild with npm, make sure that you don't specify the
174-
"--no-optional" flag. The "optionalDependencies" package.json feature is used
175-
by esbuild to install the correct binary executable for your current platform.`);
205+
"--no-optional" or "--omit=optional" flags. The "optionalDependencies" feature
206+
of "package.json" is used by esbuild to install the correct binary executable
207+
for your current platform.`);
176208
}
177209
throw e;
178210
}

0 commit comments

Comments
 (0)
Please sign in to comment.