【问题标题】:Where should the path from main in package.json point to?package.json 中 main 的路径应该指向哪里?
【发布时间】:2021-10-23 08:24:29
【问题描述】:

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.

【问题讨论】:

  • 据我所知,parcel see 的 main 作为其编译输出的目标,因此将其放在与源相同的目录中似乎不正确。应该是"main": "dist/index.html"
  • @Keith 给了我@parcel/core: Unexpected output file type .html in target "main"

标签: javascript yarnpkg parceljs


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-03-06
    • 2019-03-11
    • 2010-12-06
    • 2014-05-04
    • 2014-12-05
    • 2018-04-30
    • 1970-01-01
    • 2020-10-05
    • 2015-04-17
    相关资源
    最近更新 更多