Skip to content

Strange behavior when importing JS library #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jakelowen opened this issue Sep 4, 2021 · 4 comments
Closed

Strange behavior when importing JS library #34

jakelowen opened this issue Sep 4, 2021 · 4 comments

Comments

@jakelowen
Copy link

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
Copy link
Author

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
Copy link
Contributor

isabella commented Sep 4, 2021

@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
Copy link
Author

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
Copy link
Contributor

nitsky commented Sep 22, 2021

@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:

import-js/eslint-plugin-import#1868
jestjs/jest#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!

@nitsky nitsky closed this as completed Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants