modelfoxdotdev / modelfox

ModelFox makes it easy to train, deploy, and monitor machine learning models.
Other
1.46k stars 63 forks source link

Node import issue #4

Closed westn closed 3 years ago

westn commented 3 years ago

There seems to be an error on the Node example (https://www.tangram.xyz/docs/getting_started/predict/node).

Reproduce:

  1. run: npm install @tangramxyz/tangram
  2. create an index.js file with the provided code
  3. test and run with: node index.js
  4. Note the error below

Node-version 16.4.2

node:internal/modules/cjs/loader:1112
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/fnordell/Documents/private/machine_learning/node_modules/@tangramxyz/tangram/dist/node.js
require() of ES modules is not supported.
require() of /Users/fnordell/Documents/private/machine_learning/node_modules/@tangramxyz/tangram/dist/node.js from /Users/fnordell/Documents/private/machine_learning/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename node.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/fnordell/Documents/private/machine_learning/node_modules/@tangramxyz/tangram/package.json.

    at new NodeError (node:internal/errors:363:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:13)
    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.<anonymous> (/Users/fnordell/Documents/private/machine_learning/index.js:3:17)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32) {
  code: 'ERR_REQUIRE_ESM'
}
nitsky commented 3 years ago

@westn thanks for filing this issue!

In version 0.5.1, we did not support commonjs modules but forgot to update the documentation on the website to reflect that. We just published a new version (0.5.3) that now supports commonjs modules in addition to ES modules by using the exports key in package.json. If you update to the latest version of @tangramxyz/tangram (0.5.3) everything should work as shown in the documentation. We also added examples in languages/javascript/examples/node in this repo demonstrating the use of both commonjs and ES modules. The example in the docs has also been updated with a small change on line 9: the .buffer field needs to be used from the result of readFileSync. Please incorporate that into your code. Let us know if you run into more problems.

Thank you!

@niklaszantner

westn commented 3 years ago

Thanks! Now it's working as intended. Closing this issue.