Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Bundling sharp doesn't work anymore since updating to 4.5.0 #1075

Closed

Description

@pkeuter

Since updating to version 4.5.0 it is not possible to bundle sharp anymore. Previously this was fairly simple, by just copying the node_modules/sharp folder next to the build output. But this doesn't work anymore giving the following error:

Error:
Something went wrong installing the "sharp" module

File '/**/test/node_modules/sharp/build/Release/libvips-cpp.so.42' was not included into executable at compilation stage. Please recompile adding it as asset or script.

- Remove the "node_modules/sharp" directory then run
  "npm install --ignore-scripts=false --verbose" and look for errors
- Consult the installation documentation at https://sharp.pixelplumbing.com/install
- Search for this error at https://github.com/lovell/sharp/issues

    at Object.<anonymous> (/snapshot/test/node_modules/sharp/lib/constructor.js:34:9)
    at Module._compile (pkg/prelude/bootstrap.js:1329:22)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1218:10)
    at Module.load (internal/modules/cjs/loader.js:1047:32)
    at Function.Module._load (internal/modules/cjs/loader.js:935:14)
    at Module.require (internal/modules/cjs/loader.js:1087:19)
    at Module.require (pkg/prelude/bootstrap.js:1234:31)
    at require (internal/modules/cjs/helpers.js:73:18)
    at Object.<anonymous> (/snapshot/test/node_modules/sharp/lib/index.js:3:15)
    at Module._compile (pkg/prelude/bootstrap.js:1329:22)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1218:10)
    at Module.load (internal/modules/cjs/loader.js:1047:32)
    at Function.Module._load (internal/modules/cjs/loader.js:935:14)
    at Module.require (internal/modules/cjs/loader.js:1087:19)
    at Module.require (pkg/prelude/bootstrap.js:1234:31)
    at require (internal/modules/cjs/helpers.js:73:18)

I'm fairly convinced this has to do with #837 or #1066 or maybe a combination of both.

Looking at the below error message running in CI, it seems that prebuild-install is not correctly installed or referenced, this has been added in #1066:

> [email protected]
> Fetching base Node.js binaries to PKG_CACHE_PATH
> Warning Cannot include directory %1 into executable.
  The directory must be distributed with executable as %2.
  %1: node_modules/sharp/build/Release
  %2: path-to-executable/sharp/build/Release
> Warning Cannot include directory %1 into executable.
  The directory must be distributed with executable as %2.
  %1: node_modules/sharp/vendor/lib
  %2: path-to-executable/sharp/vendor/lib
> Warning Cannot include directory %1 into executable.
  The directory must be distributed with executable as %2.
  %1: node_modules/sharp/build/Release
  %2: path-to-executable/sharp/build/Release
> Warning Cannot include directory %1 into executable.
  The directory must be distributed with executable as %2.
  %1: node_modules/sharp/vendor/lib
  %2: path-to-executable/sharp/vendor/lib
/bin/sh: 1: /builds/test/node_modules/pkg/node_modules/.bin/prebuild-install: not found
/bin/sh: 1: /builds/test/node_modules/pkg/node_modules/.bin/prebuild-install: not found

I'm mentioning both @david-mohr as well as @geekuillaume hoping they have a suggestion on how to fix this.

On a sidenote; this seems to me a breaking change so 5.0.0 would have been a more logical version for this release I suppose.

Activity

  1. david-mohr commented on Mar 23, 2021

    @david-mohr
    Contributor

    have you run yarn to ensure that prebuild-install is actually installed?

  2. jesec commented on Mar 23, 2021

    @jesec
    Contributor

    If the mentioned changes are indeed breaking, I think we would have to revert them immediately and release a new version.

    First, I would wait for a while for responses from the related parties, and see if there is a misconfiguration or if the changes can be tweaked to be non-breaking.

    @pkeuter mentioned just copying the node_modules/sharp folder next to the build output. Is this "load from outside" way previously supported?

    Any thoughts? @leerob @robertsLando

  3. leerob commented on Mar 23, 2021

    @leerob
    Contributor

    Before doing any reverting, would need to verify and confirm it is indeed a breaking change. I'm not convinced those two PRs are related yet but would love to see the investigation here.

  4. david-mohr commented on Mar 23, 2021

    @david-mohr
    Contributor

    prebuild-install is definitely in the package.json. @pkeuter how did you upgrade and does running 'yarn' or npm i fix the issue?

  5. pkeuter commented on Mar 23, 2021

    @pkeuter
    Author

    I see it is in the package.json and it is also in the package-lock.json (I'm using npm):

    "pkg": {
          "version": "4.5.0",
          "resolved": "https://registry.npmjs.org/pkg/-/pkg-4.5.0.tgz",
          "integrity": "sha512-TEAQAbrd4PKyEydg28OLzg61L/NsGN1NcCqBFzY9TxzpPAds/Nogd+9bc2r/nZxBQhHLL1l+LldVNRFoQhdPWg==",
          "dev": true,
          "requires": {
            "@babel/parser": "^7.9.4",
            "@babel/runtime": "^7.9.2",
            "chalk": "^3.0.0",
            "escodegen": "^1.14.1",
            "fs-extra": "^8.1.0",
            "globby": "^11.0.0",
            "into-stream": "^5.1.1",
            "minimist": "^1.2.5",
            "multistream": "^2.1.1",
            "pkg-fetch": "^2.6.9",
            "prebuild-install": "6.0.1",
            "progress": "^2.0.3",
            "resolve": "^1.15.1",
            "stream-meter": "^1.0.4"
          },
    ...
    

    and this is top-level the prebuild-install:

        "prebuild-install": {
          "version": "6.0.1",
          "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-6.0.1.tgz",
          "integrity": "sha512-7GOJrLuow8yeiyv75rmvZyeMGzl8mdEX5gY69d6a6bHWmiPevwqFw+tQavhK0EYMaSg3/KD24cWqeQv1EWsqDQ==",
          "requires": {
            "detect-libc": "^1.0.3",
            "expand-template": "^2.0.3",
            "github-from-package": "0.0.0",
            "minimist": "^1.2.3",
            "mkdirp-classic": "^0.5.3",
            "napi-build-utils": "^1.0.1",
            "node-abi": "^2.7.0",
            "noop-logger": "^0.1.1",
            "npmlog": "^4.0.1",
            "pump": "^3.0.0",
            "rc": "^1.2.7",
            "simple-get": "^3.0.3",
            "tar-fs": "^2.0.0",
            "tunnel-agent": "^0.6.0",
            "which-pm-runs": "^1.0.0"
          },
          "dependencies": {
            "simple-get": {
              "version": "3.1.0",
              "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-3.1.0.tgz",
              "integrity": "sha512-bCR6cP+aTdScaQCnQKbPKtJOKDp/hj9EDLJo3Nw4y1QksqaovlW/bnptB6/c1e+qmNIDHRK+oXFDdEqBT8WzUA==",
              "requires": {
                "decompress-response": "^4.2.0",
                "once": "^1.3.1",
                "simple-concat": "^1.0.0"
              }
            }
          }
        },
    

    This was running in CI so there was no node_modules folder. No cache or something either.

    When I look in the folder referenced /home/peter/test/node_modules/pkg/node_modules/.bin/ I don't see the binary either. I do see it a bit higher in /home/peter/test/node_modules/.bin. This probably is because of the flattening procedure that npm uses (see https://docs.npmjs.com/cli/v7/configuring-npm/folders). I am not sure if this differs from yarn but this seems to be a logical structure.

  6. david-mohr commented on Mar 23, 2021

    @david-mohr
    Contributor

    @pkeuter ah yes, I see what you mean now. Here's the target reference https://github.com/vercel/pkg/blob/master/lib/producer.js#L100. As you say, yarn handles this differently which is why it worked fine during testing. I also have my pkg install globally.

    If you know how to change the path to handle any combination of situations (local/global, yarn/npm) that would be great.

  7. pkeuter commented on Mar 24, 2021

    @pkeuter
    Author

    @david-mohr I do not know a solution right away, but the fact is that this is, indeed, a breaking change so it might be useful to revert the change in a new release until this has been resolved. Because pkg is currently broken for npm users that use native modules.

  8. david-mohr commented on Mar 24, 2021

    @david-mohr
    Contributor

    @pkeuter I think this can be patched to work with both npm and yarn, but if it needs to be backed out, that's fine too.

    @leerob guess it's your call. To fix this, the function can use existsSync to hunt around for the path. Otherwise just back it out, it doesn't seem that other users are interested in cross-platform support, so it's not really a helpful patch.

  9. leerob commented on Mar 25, 2021

    @leerob
    Contributor

    I'm sorry you're having this issue @pkeuter ☹️ It currently appears it's isolated to one report. Given that, I'd say we patch forward. Cross-platform support seems like something we would want to support.

  10. JeffJassky commented on Mar 26, 2021

    @JeffJassky

    The prebuild-install problem:

    The path to the prebuild script is being built incorrectly. The error is in producer.js

    const prebuild = path.join(

    A Workaround:

    I got it to resolve the correct path by changing that line to: const prebuild = _path.default.join(process.cwd(), './node_modules/.bin/prebuild-install');. After that, the prebuild-install script path was correct - and it was being executed successfully.

    However... The prebuild script shows a warning prebuild-install WARN install No prebuilt binaries found (target=v12.2.0 runtime=node arch=x64 libc= platform=win32)

    Manually Installing cross-platform prebuilt binaries:

    So I installed my native module (sqlite3) manually using npm install sqlite3 --arch=x64 --platform=win32 which resulted in a very happy looking Success: "/Users/jeffjassky/Projects/StoryFolderDesktop/node_modules/sqlite3/lib/binding/napi-v3-win32-x64/node_sqlite3.node" is installed via remote

    Unfortunately:

    When running pkg I still got the same error: prebuild-install WARN install No prebuilt binaries found (target=v12.2.0 runtime=node arch=x64 libc= platform=win32) as if it doesn't recognize or include the newly installed module.

    My guess is that the target version was incorrect - but it's hard to know for sure. When I tried adding the --target=v12.2.0 flag I got a nasty Unsupported target version: v12.2.0 error.

    So I tried manually added the module as an asset in package.json

      "pkg": {
        "assets": [
          "./node_modules/sqlite3/lib/binding/napi-v3-win32-x64/node_sqlite3.node"
        ]
      }
    

    When I run my packaged .exe on Windows I can see that the file indeed gets included, but I get this error: Caught exception: Error: The specified module could not be found. C:\snapshot\StoryFolderDesktop\node_modules\sqlite3\lib\binding\napi-v3-win32-x64\node_sqlite3.node

    So, lastly, I copied the node_sqlite3.node file into path-to-executable/node_modules/sqlite3/lib/binding/napi-v3-win32-x64/node_sqlite3.node along side the executable and that finally seems to work, as inelegant as it is.

    So, for some reason, it can't locate the module even though it's right there in the snapshot - but it works when the module is placed alongside it.

  11. JeffJassky commented on Mar 26, 2021

    @JeffJassky

    @robertsLando I sort-of understand. These are package-specific configs for pkg - though I don't precisely know what I should put in there - 'cause I don't quite understand what the problem is, to begin with.

  12. robertsLando commented on Mar 26, 2021

    @robertsLando
    Contributor

    I'm not the author of this package I have just take a general lookup of the code and from what I have understand that's a collection of package-specific fixes. With that you can specify patches (replace/add/remove code from files) and or additional assets/scripts to add for a specific package like:

    'use strict';
    
    module.exports = {
      pkg: {
        scripts: ['lib/middleware/*.js'],
        assets: [
          'lib/public/**/*', // for [email protected]
        ],
      },
    };

    In your case I think a possible fix could be adding the assets needed here in the sqlite3 dictionary, something like:

    'use strict';
    
    module.exports = {
      pkg: {
        assets: [
          'lib/binding/**/*',
        ],
      },
    };

    Give this a try and let me know :)

  13. JeffJassky commented on Mar 26, 2021

    @JeffJassky

    @robertsLando After looking into it further, the .node module is actually getting included in the snapshot - in the exact correct location. Hmm... the plot thickens.

    Seems to be related directly to #1075, possibly #894, and #687

  14. robertsLando commented on Mar 26, 2021

    @robertsLando
    Contributor

    We might check where the lookup fails so. Based on what I understand native node modules behaves different then normal assets, them are copied in a temporary folder iin order to be used, maybe the lookup or the copy fails somewhere. That feature has been added in #837. Did you alo try to put the node addon in the same root dir of the application to see if that works?

    The magic happens here: https://github.com/vercel/pkg/pull/837/files#diff-0c3f42fd6825d8d5b4d0422402a89ff11dc1d1cfb52ee5901f4774e503783c27R1598

  15. 45 remaining items

  16. rightaway commented on Jul 24, 2021

    @rightaway

    @avpavp No luck. @robertsLando says it should work in the latest version but that's definitely not true.

  17. avpavp commented on Jul 24, 2021

    @avpavp

    @rightaway Same experience here. No success with [email protected] (current version at time of writing)

    By using bits and pieces of @webdevog repo mentioned above, I seem to be making progress using [email protected]. My exe runs now, I just need to make sure everything is actually working...

  18. john-yick commented on Aug 10, 2021

    @john-yick

    I see the same issue with [email protected] it throws the as shown below, code runs fine from node directly:

    Error: 
    Something went wrong installing the "sharp" module
    
    dlopen(/var/folders/tj/pr2229k96fsby1b59lw61s080000gn/T/e6aead472f8db8d35231c777b2f4218832660a817248e233a31dbf49c8ebc167/sharp.node, 1): Library not loaded: @rpath/libvips-cpp.42.dylib
      Referenced from: /var/folders/tj/pr2229k96fsby1b59lw61s080000gn/T/e6aead472f8db8d35231c777b2f4218832660a817248e233a31dbf49c8ebc167/sharp.node
      Reason: image not found
    
    - Remove the "node_modules/sharp" directory then run
      "npm install --ignore-scripts=false --verbose" and look for errors
    - Consult the installation documentation at https://sharp.pixelplumbing.com/install
    - Search for this error at https://github.com/lovell/sharp/issues
    
        at Object.<anonymous> (/snapshot/modv-core/node_modules/sharp/lib/constructor.js:34:9)
        at Module._compile (pkg/prelude/bootstrap.js:1751:22)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
        at Module.load (internal/modules/cjs/loader.js:863:32)
        at Function.Module._load (internal/modules/cjs/loader.js:708:14)
        at Module.require (internal/modules/cjs/loader.js:887:19)
        at Module.require (pkg/prelude/bootstrap.js:1676:31)
        at require (internal/modules/cjs/helpers.js:74:18)
        at Object.<anonymous> (/snapshot/modv-core/node_modules/sharp/lib/index.js:3:15)
        at Module._compile (pkg/prelude/bootstrap.js:1751:22)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
        at Module.load (internal/modules/cjs/loader.js:863:32)
        at Function.Module._load (internal/modules/cjs/loader.js:708:14)
        at Module.require (internal/modules/cjs/loader.js:887:19)
        at Module.require (pkg/prelude/bootstrap.js:1676:31)
        at require (internal/modules/cjs/helpers.js:74:18)
        at Object.<anonymous> (/snapshot/modv-core/api/models/common.js)
        at Module._compile (pkg/prelude/bootstrap.js:1751:22)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
        at Module.load (internal/modules/cjs/loader.js:863:32)
        at Function.Module._load (internal/modules/cjs/loader.js:708:14)
        at Module.require (internal/modules/cjs/loader.js:887:19)
    
  19. leadwolf commented on Aug 15, 2021

    @leadwolf

    Looking at the sharp repo, in binding.gyp they link to libvips-cpp.so.42 with a relative path:

    'ldflags': [
                    # Ensure runtime linking is relative to sharp.node
                    '-Wl,-s -Wl,--disable-new-dtags -Wl,-rpath=\'$$ORIGIN/../../vendor/<(vips_version)/lib\''
                  ]
    

    The problem is that when importing sharp.node, pkg copies it to a temporary folder and libvips can't be resolved from that location. As fallback, pkg tries to find the missing file in the same virtual directory as the .node file, and copies it to the temporary folder. This won't work because at build time libvips isn't in the same directory as sharp.node, so it won't end up in the same virtual directory either. But even if we did manage to get libvips into the same virtual directory and have pkg copy it to the temporary folder, the import will still not work because it is not at the expected relative path: ../../vendor....

  20. robertsLando commented on Sep 29, 2021

    @robertsLando
    Contributor

    @leadwolf I think you detected the real problem. I also tried to move sharp into the same directory with no luck, I also tried adding both folders to assets:

    "assets": [
           "./node_modules/sharp/build/Release/**",
          "./node_modules/sharp/vendor/**",
        ]
    

    and the entire folder:

    "assets": [
           "./node_modules/sharp/**",
        ]
    

    Did you find a workaround to this? @jesec @erossignon suggestions?

    This should be handled here: https://github.com/vercel/pkg/blob/main/prelude/bootstrap.js#L2035 but for some reason it's not working as expected

  21. robertsLando commented on Sep 29, 2021

    @robertsLando
    Contributor

    IMO what we should do is to copy the entire module folder instead of just the module, I will see if I can make a PR

  22. added a commit that references this issue on Sep 29, 2021
    988397d
  23. robertsLando commented on Sep 29, 2021

    @robertsLando
    Contributor

    For anyone interested in this a fix is available with #1321

  24. added a commit that references this issue on Sep 30, 2021
    165617a
  25. added a commit that references this issue on Dec 18, 2021
    a23b3ae
  26. rightaway commented on Jul 15, 2022

    @rightaway

    @robertsLando Is it still working for you? An error is happening again #1692.

  27. robertsLando commented on Jul 15, 2022

    @robertsLando
    Contributor

    It's working for me. Try using

    "assets": [
      "node_modules/sharp/**",
    ]
    
  28. rightaway commented on Jul 16, 2022

    @rightaway

    @robertsLando I tried again and it's actually working even without "assets". But when building I get these warnings. The same happened in #229. Do you know the cause?

    > [email protected]
    compression:  GZip
    > Warning Cannot include directory %1 into executable.
      The directory must be distributed with executable as %2.
      %1: node_modules/sharp/build/Release
      %2: path-to-executable/sharp/build/Release
    > Warning Cannot include directory %1 into executable.
      The directory must be distributed with executable as %2.
      %1: node_modules/sharp/vendor/lib
      %2: path-to-executable/sharp/vendor/lib
    > Warning Cannot include directory %1 into executable.
      The directory must be distributed with executable as %2.
      %1: node_modules/sharp/build/Release
      %2: path-to-executable/sharp/build/Release
    > Warning Cannot include directory %1 into executable.
      The directory must be distributed with executable as %2.
      %1: node_modules/sharp/vendor/lib
      %2: path-to-executable/sharp/vendor/lib
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions