Skip to content

Commit 12a6895

Browse files
committed
fix: fix require.resolve on older Node versions
1 parent 0857062 commit 12a6895

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

script/install.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,38 @@ function cmakeTs() {
55
console.log(
66
"Building addon node via cmake-ts (requires cmake, ninja, and the vcpkg dependencies)",
77
)
8-
const cmakeTsPath = require.resolve("cmake-ts/build/main.js")
8+
let cmakeTsPath = tryRequireResolve("cmake-ts/build/main.js")
9+
if (cmakeTsPath === undefined) {
10+
cmakeTsPath = tryRequireResolve("cmake-ts/build/main")
11+
}
12+
if (cmakeTsPath === undefined) {
13+
throw new Error(
14+
"Failed to find cmake-ts in cmake-ts/build/main.js or cmake-ts/build/main.js",
15+
)
16+
}
917

1018
cp.execFileSync(process.execPath, [cmakeTsPath, "nativeonly"], {
1119
stdio: "inherit",
1220
})
1321
}
1422

23+
/**
24+
* Try to require resolve a path.
25+
* @param {string} path
26+
* @returns {string | undefined}
27+
*/
28+
function tryRequireResolve(path) {
29+
try {
30+
return require.resolve(path)
31+
} catch (error) {
32+
return undefined
33+
}
34+
}
35+
36+
/**
37+
* Log a warning if the environment is not production.
38+
* @param {string} message
39+
*/
1540
function devWarn(message) {
1641
if (process.env.NODE_ENV !== "production") {
1742
console.warn(message)

0 commit comments

Comments
 (0)