thiagoelg / node-printer

Native node.js printer
132 stars 79 forks source link

Doesn't work on M1 Macs #48

Open diruuu opened 2 years ago

diruuu commented 2 years ago

I tried to use this with Electron 16, but got this error:

/node-printer/lib/node_printer.node' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))

Any idea why?

ajgassner commented 2 years ago

Because the prebuilt lib is available in x64 for darwin only. A workaround I use is to compile Electron with x64 arch using Rosetta on Silicon M1 Macs. It works but decreases the performance significantly... See https://gist.github.com/LeZuse/bf838718ff2689c5fc035c5a6825a11c

@thiagoelg Would be very nice to have prebuilt artifacts for native arm64. Is it possible somehow?

Update: It's also possible to freshly compile against arm64 on your local Silicon Mac.

AndhikaR commented 2 years ago

i already compiling using x64 with rosetta, still getting error Error: dlopen(/path-to-app/node_modules/printer/lib/node_printer.node, 0x0001): tried: '/path-to-app/node_modules/printer/lib/node_printer.node' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))

bandanacode commented 2 years ago

@ajgassner Could you be kind and give the steps to compile oagains arm64? I tried to recompile and I still get the x86_64 error despite the fact that the compilation succeeded.

duxthefux commented 1 year ago

I'm having the same issue. Is there a way to get this working already?

ajgassner commented 1 year ago

You need to build it from source. Here I will show you a minimal example with electron-rebuild. In my prduction project I use electron-builder which uses electron-rebuild under the hood. With electron-builder all the native dependencies get automatically built, there is no explict electron-rebuild required. The compiler flag CXXFLAGS=\"--std=c++17\" is important! I compiled the printer module successfully for Mac x64,arm64,universal and WIndows 32,64bit

System requirements

You can use https://github.com/felixrieseberg/windows-build-tools to install the required dependencies on Windows.

Install python2 on Mac

brew install pyenv
pyenv install 2.7.18

# in ~/.zshrc (if you are using zsh)
export PATH="$HOME/.pyenv/versions/2.7.18/bin:$PATH"

Testproject package.json

{
  "name": "printer-test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@thiagoelg/node-printer": "0.6.2"
  },
  "devDependencies": {
    "electron": "23.2.0",
    "@electron/rebuild": "^3.2.10"
  },
  "scripts": {
    "rebuild": "CXXFLAGS=\"--std=c++17\" electron-rebuild -f -w node_modules/@thiagoelg/node-printer"
  }
}

Build output

➜ yarn run rebuild
yarn run v1.22.19
$ CXXFLAGS="--std=c++17" electron-rebuild -f -w node_modules/@thiagoelg/node-printer
⠧ Building module: node-printer, Completed: 0  CXX(target) Release/obj.target/node_printer/src/node_printer_posix.o
⠙ Building module: node-printer, Completed: 0../src/node_printer_posix.cc:310:5: warning: unused variable 'isolate' [-Wunused-variable]
    MY_NODE_MODULE_HANDLESCOPE;
    ^
../src/macros.hh:12:38: note: expanded from macro 'MY_NODE_MODULE_HANDLESCOPE'
#  define MY_NODE_MODULE_HANDLESCOPE MY_NODE_MODULE_ISOLATE_DECL Nan::HandleScope scope
                                     ^
../src/macros.hh:10:52: note: expanded from macro 'MY_NODE_MODULE_ISOLATE_DECL'
#  define MY_NODE_MODULE_ISOLATE_DECL v8::Isolate* isolate = v8::Isolate::GetCurrent();
                                                   ^
⠼ Building module: node-printer, Completed: 01 warning generated.
  CXX(target) Release/obj.target/node_printer/src/node_printer.o
⠏ Building module: node-printer, Completed: 0  SOLINK_MODULE(target) Release/node_printer.node
  COPY /printer-test/node_modules/@thiagoelg/node-printer/lib/node_printer.node
  TOUCH Release/obj.target/action_after_build.stamp
✔ Rebuild Complete
✨  Done in 1.83s.

Verification

The native module is now available under /printer-test/node_modules/@thiagoelg/node-printer/lib/node_printer.node and ready to use with Electron 23.2.0.

Verify arch on Mac:

$> file /printer-test/node_modules/@thiagoelg/node-printer/lib/node_printer.node
/printer-test/node_modules/@thiagoelg/node-printer/lib/node_printer.node: Mach-O 64-bit bundle arm64