javascript - package.json 中 main 的路径应该指向哪里?

标签 javascript yarnpkg parceljs

package.json 是用 yarn cli 创建的。当我尝试构建它时它失败了。如果我删除 "main": "src/index.js" 它会起作用。 src/index.js 存在,但目前它是一个空文件。

我是否需要指定我的包的路径或者为什么会抛出错误?将路径更改为错误消息中建议的路径也不起作用。

package.json

{
  "name": "project",
  "version": "0.1.0",
  "description": "",
  "main": "src/index.js",
  "source": "src/index.html",
  "browserslist": "supports es6-module",
  "scripts": {
    "start": "parcel --open chrome",
    "build": "parcel build"
  },
  "repository": "",
  "author": "",
  "license": "MIT",
  "dependencies": {
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0"
  },
  "devDependencies": {
    "parcel": "^2.0.0-rc.0"
  }
}

输出

$ parcel build
🚨 Build failed.

@parcel/namer-default: Target "main" declares an output file path of "src/index.js" which does not match the compiled bundle type "html".

  .../project/package.json:5:11
    4 |   "description": "project",
  > 5 |   "main": "src/index.js",
  >   |           ^^^^^^^^^^^^^^ Did you mean "src/index.html"?
    6 |   "source": "src/index.html",
    7 |   "scripts": {

  💡 Try changing the file extension of "main" in package.json.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

最佳答案

package.json 中的 main 字段旨在供库开发人员使用(例如,如果您正在构建一个要发布到 npm 以供使用的包由他人)。在此上下文中,它指定了 output 目标 - 即 parcel 应该放置优化的 commonjs javascript 包的位置。 (参见 high-level documentationdetailed documentation)。

在你的情况下,你似乎正在构建一个应用程序,而不是一个库,所以我建议完全删除 main 字段,这应该可以解决你看到的错误。

关于javascript - package.json 中 main 的路径应该指向哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68884914/

相关文章:

javascript - 在光标附近添加帮助文本

javascript - Gulp HTML 和 JS 连接 - ERR_FILE_NOT_FOUND

javascript - mongodb 调试 - ReferenceError : console is not defined

javascript - 有没有办法在重新加载页面时覆盖浏览器重新发送表单提示?

node.js - 在根目录中升级 yarn 不会升级 Workspace 依赖项

node.js - 如何检测脚本是使用 npm 还是 yarn 运行?

java - 在 tomcat 上运行的 jHipster 客户端不与在开发模式下运行的服务器通信 mvnw

javascript - chrome 72 更改了 sourcemap 行为

google-chrome-extension - 我可以在 chrome 扩展中启用源映射吗?

parceljs - 有没有办法像在 Webpack 中一样在 Parcel 中代理请求?