Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Module resolution issue with "module" property #369

Closed
w33ble opened this issue Jan 27, 2018 · 7 comments
Closed

Module resolution issue with "module" property #369

w33ble opened this issue Jan 27, 2018 · 7 comments
Labels

Comments

@w33ble
Copy link

w33ble commented Jan 27, 2018

Do you want to request a feature or report a bug?

Report a bug

What is the current behavior?

Module resolution is wrong, it seems to be using the "module" property from child modules' package.json files.

If the current behavior is a bug, please provide the steps to reproduce.

I see this when attempting to use the matchit package, which requires a couple modules that ship with both "main" and "module" properties in their package.json files.

1 - npm install --dev poi; npm install matchit
2 - Add the example code from the readme to index.js
3 - npx poi
4 - Open in browser, and check the console

You'll see an error "Uncaught TypeError: filter is not a function" coming from the index.js file in matchit. Since it's falling back to the module file, it's using es6 export, but the code is expecting the node module.exports version. You can also see this in the path in the build: ./node_modules/@arr/filter/module.js, which should be ./node_modules/@arr/filter/index.js

What is the expected behavior?

Module resolution should be using ./node_modules/@arr/filter/index.js, which is set in the "main" property in the child module's package.json file.

This code works as expected when run with node, ala node index.js.

If you modify the package.json file in node_modules/@arr/filter/package.json, removing the "module" property, the code functions as expected.

I can't figure out what's causing it, so this could be a Webpack problem too. As far as I can find, Webpack doesn't automatically resolve using the "module" property, so I thought I'd start here. Maybe there's something I can configure or disable that would solve the resolution issue?

Please mention other relevant information such as the browser version, Node.js version, Poi version and Operating System.

  • Chrome 64
  • node v6.12.2
  • npm v3.10.10
  • poi v9.6.13
  • matchit v1.0.1
  • @arr/filter v1.0.0
@egoist
Copy link
Owner

egoist commented Jan 27, 2018

hmm this is actually webpack's default behavior, https://webpack.js.org/configuration/resolve/#resolve-mainfields

@egoist
Copy link
Owner

egoist commented Jan 27, 2018

seems related to webpack/webpack#4742

@w33ble
Copy link
Author

w33ble commented Jan 27, 2018

Oof, apparently I need to work on my search skills. I found a reference to the mainFields setting, but didn’t think to look up the default value. I should be able override that setting in my poi config, right? If so, pointers appreciated, and go ahead and close this issue.

@egoist
Copy link
Owner

egoist commented Jan 27, 2018

You can directly mutate the webpack config https://poi.js.org/#/home?id=webpack

@egoist
Copy link
Owner

egoist commented Jan 27, 2018

but I think it should also be solved by webpack itself 🧐

@w33ble
Copy link
Author

w33ble commented Jan 27, 2018

I think I agree with the comment that you shouldn’t be exporting different things from the cjs and es modules. My problem is upstream, and I plan to open an issue there, but it comes down to the cjs module exporting a function and the esm exporting an object. We’ll see how that Webpack issue shakes out, but that’s the spec, after all.

Thanks for input, and for creating this super useful tool.

@github-actions
Copy link

github-actions bot commented May 1, 2020

This issue has been marked as Stale, it will be closed in a week if there's no furthur activity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants