vercel / ncc

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
https://npmjs.com/@vercel/ncc
MIT License
9.09k stars 288 forks source link

can't use tensorflow js #726

Open yovanoc opened 3 years ago

yovanoc commented 3 years ago

Hello I search for a solution but I don't find why is not working with tfjs, thanks

$ ncc run src/index.ts
ncc: Version 0.28.6
ncc: Compiling file index.js
ncc: Using typescript@4.4.0-dev.20210708 (local user-provided)
    40kB  sourcemap-register.js
    92kB  lib/napi-v8/tfjs_binding.node
  6760kB  index.js
  7729kB  index.js.map
 29520kB  deps/lib/libtensorflow_framework.2.4.1.dylib
361006kB  deps/lib/libtensorflow.2.4.1.dylib
397418kB  [17155ms] - ncc 0.28.6
Error: dlopen(/private/var/folders/q3/8dq9l4050qbdnl_c4dv4vwph0000gn/T/773f3629bd23e026dbb965609402115c/lib/napi-v8/tfjs_binding.node, 0x0001): Library not loaded: @loader_path/../../deps/lib/libtensorflow.dylib
  Referenced from: /private/var/folders/q3/8dq9l4050qbdnl_c4dv4vwph0000gn/T/773f3629bd23e026dbb965609402115c/lib/napi-v8/tfjs_binding.node
  Reason: tried: '/private/var/folders/q3/8dq9l4050qbdnl_c4dv4vwph0000gn/T/773f3629bd23e026dbb965609402115c/lib/napi-v8/../../deps/lib/libtensorflow.dylib' (no such file), '/usr/local/lib/libtensorflow.dylib' (no such file), '/usr/lib/libtensorflow.dylib' (no such file)
    at Object.Module._extensions..node (node:internal/modules/cjs/loader:1154:18)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.1833 (/private/var/folders/q3/8dq9l4050qbdnl_c4dv4vwph0000gn/T/webpack:/ia-test/node_modules/@tensorflow/tfjs-node/lib/napi-v8/tfjs_binding.node:1:1)
    at __nccwpck_require__ (/private/var/folders/q3/8dq9l4050qbdnl_c4dv4vwph0000gn/T/webpack:/ia-test/webpack/bootstrap:21:1)
    at Object.348 (/private/var/folders/q3/8dq9l4050qbdnl_c4dv4vwph0000gn/T/webpack:/ia-test/node_modules/@tensorflow/tfjs-node/dist/index.js:60:1)
    at __nccwpck_require__ (/private/var/folders/q3/8dq9l4050qbdnl_c4dv4vwph0000gn/T/webpack:/ia-test/webpack/bootstrap:21:1)
    at /private/var/folders/q3/8dq9l4050qbdnl_c4dv4vwph0000gn/T/773f3629bd23e026dbb965609402115c/index.js:171760:12
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
fernandolujan commented 1 year ago

I am running into the same issue.

@yovanoc are you running this on an M1 mac by chance? Not sure if it's related -just spitballing.

yovanoc commented 1 year ago

No i9

fernandolujan commented 1 year ago

@yovanoc Do you have python 3.x or 2.x installed on your system?

After a bit of digging I found out things may not build/install properly when using 3.x. (which I am using)

I will try rebuilding with python 2.7 and report back at some point

fernandolujan commented 1 year ago

Ok, I managed to fix this issue on my machine. For reference, I am using an M1 mac, but I was getting the same error as @yovanoc so I'm thinking it may work for i9 too.

Here's what I did: 1) Downgraded to python 2.7. I didn't want to remove my existing 3.x or change my global config, so I ended up installing pyenv and running pyenv install 2.7.18 and then used pyenv local 2.7.18 in my project directory.

2) My build system is using webpack, so I ran into some errors as it tried to build the tensorflow externals. I had to add the following to my main webpack config:

// webpack.main.config.js

const nodeExternals = require('webpack-node-externals');

// ...

module.exports = {
  entry: "./src/index.js",
  // ...
  externals: [nodeExternals()],
};

Since I am using a monorepo I had to add the local namespace to the allow list:

    nodeExternals({
      allowlist: [/^@example/]
    }),

Reference: https://github.com/tensorflow/tfjs/tree/master/tfjs-node#windows--mac-os-x-requires-python-27