@@ -9,62 +9,71 @@ const { familySync, versionSync } = require('detect-libc');
9
9
10
10
const { runtimePlatformArch, prebuiltPlatforms, minimumLibvipsVersion } = require ( './libvips' ) ;
11
11
const runtimePlatform = runtimePlatformArch ( ) ;
12
- const [ isLinux , isMacOs , isWindows ] = [ 'linux' , 'darwin' , 'win32' ] . map ( os => runtimePlatform . startsWith ( os ) ) ;
13
12
14
- /* istanbul ignore next */
15
- try {
16
- // Check for local build
17
- module . exports = require ( `../src/build/Release/sharp-${ runtimePlatform } .node` ) ;
18
- } catch ( errLocal ) {
13
+ const paths = [
14
+ `../src/build/Release/sharp-${ runtimePlatform } .node` ,
15
+ `@img/sharp-${ runtimePlatform } /sharp.node`
16
+ ] ;
17
+
18
+ const errors = [ ] ;
19
+ for ( const path of paths ) {
19
20
try {
20
- // Check for runtime package
21
- module . exports = require ( `@img/sharp-${ runtimePlatform } /sharp.node` ) ;
22
- } catch ( errPackage ) {
23
- const help = [ 'Could not load the "sharp" module at runtime' ] ;
24
- if ( errLocal . code !== 'MODULE_NOT_FOUND' ) {
25
- help . push ( `${ errLocal . code } : ${ errLocal . message } ` ) ;
26
- }
27
- if ( errPackage . code !== 'MODULE_NOT_FOUND' ) {
28
- help . push ( `${ errPackage . code } : ${ errPackage . message } ` ) ;
29
- }
30
- help . push ( 'Possible solutions:' ) ;
31
- // Common error messages
32
- if ( prebuiltPlatforms . includes ( runtimePlatform ) ) {
33
- const [ os , cpu ] = runtimePlatform . split ( '-' ) ;
34
- help . push ( '- Add an explicit dependency for the runtime platform:' ) ;
35
- help . push ( ` npm install --os=${ os } --cpu=${ cpu } sharp` ) ;
36
- help . push ( ' or' ) ;
37
- help . push ( ` npm install --force @img/sharp-${ runtimePlatform } ` ) ;
38
- } else {
39
- help . push ( `- The ${ runtimePlatform } platform requires manual installation of libvips >= ${ minimumLibvipsVersion } ` ) ;
40
- }
41
- if ( isLinux && / s y m b o l n o t f o u n d / i. test ( errPackage ) ) {
42
- try {
43
- const { engines } = require ( `@img/sharp-libvips-${ runtimePlatform } /package` ) ;
44
- const libcFound = `${ familySync ( ) } ${ versionSync ( ) } ` ;
45
- const libcRequires = `${ engines . musl ? 'musl' : 'glibc' } ${ engines . musl || engines . glibc } ` ;
46
- help . push ( '- Update your OS:' ) ;
47
- help . push ( ` Found ${ libcFound } ` ) ;
48
- help . push ( ` Requires ${ libcRequires } ` ) ;
49
- } catch ( errEngines ) { }
50
- }
51
- if ( isMacOs && / I n c o m p a t i b l e l i b r a r y v e r s i o n / . test ( errLocal . message ) ) {
52
- help . push ( '- Update Homebrew:' ) ;
53
- help . push ( ' brew update && brew upgrade vips' ) ;
54
- }
55
- if ( errPackage . code === 'ERR_DLOPEN_DISABLED' ) {
56
- help . push ( '- Run Node.js without using the --no-addons flag' ) ;
57
- }
58
- if ( process . versions . pnp ) {
59
- help . push ( '- Use a supported yarn linker, either pnpm or node-modules:' ) ;
60
- help . push ( ' yarn config set nodeLinker node-modules' ) ;
61
- }
62
- // Link to installation docs
63
- if ( isWindows && / T h e s p e c i f i e d p r o c e d u r e c o u l d n o t b e f o u n d / . test ( errPackage . message ) ) {
64
- help . push ( '- Using the canvas package on Windows? See https://sharp.pixelplumbing.com/install#canvas-and-windows' ) ;
65
- } else {
66
- help . push ( '- Consult the installation documentation: https://sharp.pixelplumbing.com/install' ) ;
21
+ module . exports = require ( path ) ;
22
+ break ;
23
+ } catch ( err ) {
24
+ /* istanbul ignore next */
25
+ errors . push ( err ) ;
26
+ }
27
+ }
28
+
29
+ /* istanbul ignore next */
30
+ if ( ! module . exports ) {
31
+ const [ isLinux , isMacOs , isWindows ] = [ 'linux' , 'darwin' , 'win32' ] . map ( os => runtimePlatform . startsWith ( os ) ) ;
32
+
33
+ const help = [ `Could not load the "sharp" module using the ${ runtimePlatform } runtime` ] ;
34
+ errors . forEach ( err => {
35
+ if ( err . code !== 'MODULE_NOT_FOUND' ) {
36
+ help . push ( `${ err . code } : ${ err . message } ` ) ;
67
37
}
68
- throw new Error ( help . join ( '\n' ) ) ;
38
+ } ) ;
39
+ const messages = errors . map ( err => err . message ) . join ( ' ' ) ;
40
+ help . push ( 'Possible solutions:' ) ;
41
+ // Common error messages
42
+ if ( prebuiltPlatforms . includes ( runtimePlatform ) ) {
43
+ const [ os , cpu ] = runtimePlatform . split ( '-' ) ;
44
+ help . push ( '- Add platform-specific dependencies:' ) ;
45
+ help . push ( ` npm install --os=${ os } --cpu=${ cpu } sharp` ) ;
46
+ help . push ( ' or' ) ;
47
+ help . push ( ` npm install --force @img/sharp-${ runtimePlatform } ` ) ;
48
+ } else {
49
+ help . push ( `- Manually install libvips >= ${ minimumLibvipsVersion } ` ) ;
50
+ }
51
+ if ( isLinux && / s y m b o l n o t f o u n d / i. test ( messages ) ) {
52
+ try {
53
+ const { engines } = require ( `@img/sharp-libvips-${ runtimePlatform } /package` ) ;
54
+ const libcFound = `${ familySync ( ) } ${ versionSync ( ) } ` ;
55
+ const libcRequires = `${ engines . musl ? 'musl' : 'glibc' } ${ engines . musl || engines . glibc } ` ;
56
+ help . push ( '- Update your OS:' ) ;
57
+ help . push ( ` Found ${ libcFound } ` ) ;
58
+ help . push ( ` Requires ${ libcRequires } ` ) ;
59
+ } catch ( errEngines ) { }
60
+ }
61
+ if ( isMacOs && / I n c o m p a t i b l e l i b r a r y v e r s i o n / . test ( messages ) ) {
62
+ help . push ( '- Update Homebrew:' ) ;
63
+ help . push ( ' brew update && brew upgrade vips' ) ;
64
+ }
65
+ if ( errors . some ( err => err . code === 'ERR_DLOPEN_DISABLED' ) ) {
66
+ help . push ( '- Run Node.js without using the --no-addons flag' ) ;
67
+ }
68
+ if ( process . versions . pnp ) {
69
+ help . push ( '- Use a supported yarn linker, either pnpm or node-modules:' ) ;
70
+ help . push ( ' yarn config set nodeLinker node-modules' ) ;
71
+ }
72
+ // Link to installation docs
73
+ if ( isWindows && / T h e s p e c i f i e d p r o c e d u r e c o u l d n o t b e f o u n d / . test ( messages ) ) {
74
+ help . push ( '- Using the canvas package on Windows? See https://sharp.pixelplumbing.com/install#canvas-and-windows' ) ;
75
+ } else {
76
+ help . push ( '- Consult the installation documentation: https://sharp.pixelplumbing.com/install' ) ;
69
77
}
78
+ throw new Error ( help . join ( '\n' ) ) ;
70
79
}
0 commit comments