fix macos image not found in node native binding


Problem

The workspace primary folder structure are as followed:

/my/native-binding/
├── lib
│   └── mac
│       └── binding.node
└── sdk
    └── sdk.framework

The problem is: when try to directly require this binding.node and dynamically load the sdk.framework in the sdk folder, the terminal always throws following error:

internal/modules/cjs/loader.js:1144
  return process.dlopen(module, path.toNamespacedPath(filename));

Error: dlopen(/my/native-binding/lib/mac/binding.node, 1): Library not loaded: @rpath/sdk.framework/Versions/A/sdk
  Referenced from: /my/native-binding/lib/mac/binding.node
  Reason: image not found
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1144:18)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    ...

otool

Check an installed electron app:

$ otool -l /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron
/Applications/Visual Studio Code.app/Contents/MacOS/Electron:
Load command 24
          cmd LC_RPATH
      cmdsize 48
         path @executable_path/../Frameworks (offset 12)
Load command 25
      cmd LC_CODE_SIGNATURE
  cmdsize 16
  dataoff 349216
 datasize 38608

dyld

install_name_tool

$ install_name_tool -add_rpath /my/native-binding/sdk/ /my/native-binding/lib/mac/binding.node

spctl

$ sudo spctl --master-disable

xattr

$ sudo xattr -r -d com.apple.quarantine ./lib/sdk.framework

This will remove the quarantine flag from the framework, which is preventing the framework from being run.

references