Skip to content
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

Can't resolve dependency when it has only the browser field #32714

Closed
macabeus opened this issue Dec 21, 2021 · 4 comments
Closed

Can't resolve dependency when it has only the browser field #32714

macabeus opened this issue Dec 21, 2021 · 4 comments
Labels
bug Issue was opened via the bug report template.

Comments

@macabeus
Copy link

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

v12.18.4

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

The error happens locally

Describe the Bug

If a dependency doesn't have the field main in the package.json, the error Module not found: Can't resolve 'dependency-name' is raised.

Expected Behavior

It should work since a dependency could not have the field main if it's only for the browser. In this case, the field browser is filled.

NPM doc is reasonable when explaining that it's an "instead of". Otherwise, I'm losing the hint that it's only for the browser.

browser
If your module is meant to be used client-side the browser field should be used instead of the main field. This is helpful to hint users that it might rely on primitives that aren't available in Node.js modules. (e.g. window)

To Reproduce

I created an example repository: https://github.com/macabeus/next-js-react-gba-js

> git clone git@github.com:macabeus/next-js-react-gba-js.git
> cd next-js-react-gba-js
> npm run dev

Everything works well.

But if you downgrade the package react-gbajs to 1.0.0, or remove the field main at node_modules/react-gbajs/package.json, the error is raised.

@macabeus macabeus added the bug Issue was opened via the bug report template. label Dec 21, 2021
@macabeus macabeus changed the title Can't resolve dependecy when it have only the browser field Can't resolve dependency when it have only the browser field Dec 21, 2021
@macabeus macabeus changed the title Can't resolve dependency when it have only the browser field Can't resolve dependency when it has only the browser field Dec 21, 2021
@balazsorban44
Copy link
Member

When using Next.js 12, the minimum required Node version is 12.22.0: https://nextjs.org/docs/upgrading#minimum-nodejs-version

I can also see that you are the maintainer of the package, so I suggest reading this discussion to make sure you bundle/publish the package according to the standards: #31518 (comment)

browser is not a real standard.

@macabeus
Copy link
Author

macabeus commented Dec 22, 2021

browser is not a real standard.

So the NPM documentation is wrong?
It's pretty clear that if the package is only for the browser, it should use browser instead of main. If NPM documentation is wrong, where can I read more about the real standard for package.json?

I can also see that you are the maintainer of the package, so I suggest reading this discussion to make sure you bundle/publish the package according to the standards: #31518 (comment)

I read the discussion starting from the comment that you linked, and it looks like that Pauan is correct when complaining that Nextjs is breaking the standard. I updated my package because it's my toy project, but I don't know if other libraries author are going to do the same.


Furthermore, I had a similar discussion on eslint-plugin-import (import-js/eslint-plugin-import#2132), and in the end, it was marked as an issue in eslint-plugin-import, not in my project.

@balazsorban44
Copy link
Member

So to be more precise.

We don't support when only browser is defined, as we don't run packages only in the browser. Defining a main is a good idea.

So the NPM documentation is wrong?

browser is just a convention that became popular, we do support it but only in the browser compilation, not in the Node.js compilation (logically)

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

2 participants