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

package.json里的main,browser,module字段 #86

Open
gdutwyg opened this issue Oct 31, 2019 · 0 comments
Open

package.json里的main,browser,module字段 #86

gdutwyg opened this issue Oct 31, 2019 · 0 comments

Comments

@gdutwyg
Copy link
Owner

gdutwyg commented Oct 31, 2019

当我们引入一个npm包的时候,我们知道在js是怎么写的,如果是浏览器就import xxx from 'xxx', 如果在node里面就是const xxx = require('xxx'),大家应该也知道引入一个npm包,npm会从package.json里面的 main 字段读取文件。

image

但是现在我又学到了可能还会有browsermodule字段,这三个字段都可以用来当作npm读取文件的凭据,那它们优先级如何呢?

image

main

定义了 npm 包的入口文件

module

定义一个针对 es6 模块及语法的入口文件

browser

指定供浏览器使用的入口文件

总结

  • 如果是在浏览器的web项目,优先级是browser > module > main
  • 如果是在node的web下项目, browser字段是无效的
    • 如果node使用了webpack,那优先级就是 module > main
    • 如果没有使用webpack,则只有main字段有效。

参考

package.json 中 你还不清楚的 browser,module,main 字段优先级

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

No branches or pull requests

1 participant