tfjs: tfjs-node "Illegal instruction: 4" error on macOS Monterey (M1)

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS 12.1 (Monterey), 2020 MacBook Air M1
  • TensorFlow.js installed from (npm or script link): npm
  • TensorFlow.js version: “@tensorflow/tfjs”: “^3.16.0”, “@tensorflow/tfjs-node”: “^3.16.0”

Describe the problem In an empty project, installing and requiring @tensorflow/tfjs-node results in an “Illegal instruction: 4” error thrown.

Provide the exact sequence of commands / steps that you executed before running into the problem

  • Create an empty project (npm init)
  • yarn add @tensorflow/tfjs-node in project root
  • create an index.js file and add const tf = require('@tensorflow/tfjs-node') at the top of the file
  • node index.js in project root
  • observe error “Illegal instruction: 4” thrown

As reported in the closed https://github.com/tensorflow/tfjs/issues/4514 ticket, tfjs-node doesn’t appear to work with macOS Monterey. Is there any way to make it work?

Would appreciate any tips, cheers!

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 16

Most upvoted comments

Hi @MikeOBarometer @Caundy I have tried the setup Mike mentioned, it is also working for me. There are couple caveats you need to pay attention to:

  1. Make sure you are starting your terminal without rosetta. (Which make sure the arch is in arm64)
  2. use nvm to install node with arm64 binary.
  3. install tfjs-node after that.

This way, your node environment arch matches with tfjs-node binding and TF download. And x86-64 arch that enabled by rosetta would not work with tensorflow (the illegal instruction 4 error), even though all archs match. Please let me know if this would work for you.

Strange! Let me know if I can provide any other system info if it helps. Here’s my versions + arch

$ node -v && npm -v && npm info @tensorflow/tfjs-node version && uname -m
v16.15.1
8.11.0
3.18.0
arm64