modelfoxdotdev / modelfox

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

Strange behavior when importing JS library #34

Closed jakelowen closed 2 years ago

jakelowen commented 2 years ago

After I install via npm install @tangramdotdev/tangram and attempt to import using let tangram = require("@tangramdotdev/tangram") and follow the remainder of the tutorial steps

I can run the script and get a working prediction as expected. However I am noticing some strange behaviors.

A) - Eslint is not happy with the import. It always gives me "Unable to resolve path to module '@tangramdotdev/tangram'.eslintimport/no-unresolved"

B) Even though the script will execute and work as expected, I can not test any function that imports the tangram library. Jest always throws the "Cannot find module '@tangramdotdev/tangram' from 'index.js' "

I've never observed this before in any other library. Any guidance? I've included some screenshots:

Working script: (Note the ESLINT error on import) image Ignore blocks at line 13 and 161 - Just some data marshalling / cleaning to get it in the right shape.

Working result: (after directly uncommenting line 177 in above image to run directly) image

A simple jest test of above foo function:

const foo = require("./index")

test("foo", () => {
    expect(foo()).toEqual({})
})

But when I run above test: image

It's so baffling to me that I can run the script and all works as normal, but ESLINT and Jest are both throwing fits.

jakelowen commented 2 years ago

After reading through the installed module in my node_modules folder, I figured out a workaround. If I change my import statement to the full path: const tangram = require("@tangramdotdev/tangram/dist/node/index.cjs") then all eslint errors disappear and tests run just fine.

Hope that helps.

isabella commented 2 years ago

@jakelowen thank you so much for the detailed bug report. We will investigate this tomorrow morning. I think this may be caused by eslint and jest not respecting the package.json exports key, which only works in newer versions on node. What Node.js, eslint, and jest versions are you using?

jakelowen commented 2 years ago

Thanks @isabella. No particular rush on my side to resolve. Enjoy your labor day weekend!

Versions: Node: 14.17.6 eslint: "^7.32.0", jest: "^27.1.0", MAC OS X: 11.5.2

nitsky commented 2 years ago

@jakelowen thanks for opening this issue. I was able to reproduce it, find the cause, and implement a solution. Your detailed report was very helpful.

We want to have a single npm package that supports commonjs, esm, node, and bundlers. We do this using the new "exports" field in package.json. Node has supported this feature for a while, but not all tools have added support. Here are the open issues in eslint-plugin-import and jest:

https://github.com/import-js/eslint-plugin-import/issues/1868 https://github.com/facebook/jest/issues/9771

As a workaround, I added in the old "main" key as a fallback. I published a patch release (v0.7.1) of the tangram npm package, so if you npm update it should start working. Let us know if you are still having trouble!