简体   繁体   English

Parcel 什么都不做,不被识别为命令

[英]Parcel does nothing and is not recognized as a command

I installed Parcel with npm, everything installed fine without errors.我用 npm 安装了 Parcel,一切都安装得很好,没有错误。 I have a package.json like this:我有一个这样的 package.json:

{
    "name": "example",
    "version": "0.1.0",
    "source": "src/index.js",
    "main": "dist/index.js",
    "scripts": {
        "build": "parcel build"
    },
    "devDependencies": {
        "parcel": "^2.0.1"
    }
}

I'm just following the instructions on the website, but when I run npm run build it just says:我只是按照网站上的说明进行操作,但是当我运行npm run build它只是说:

Debugger attached.附上调试器。 Waiting for the debugger to disconnect...等待调试器断开连接...

Then it goes back to the command prompt.然后它返回到命令提示符。 No files are touched or created.没有触及或创建任何文件。 And actually parcel is not recognized as a command in the terminal.实际上, parcel在终端中不被识别为命令。 I tried uninstalling and re-installing Parcel but it still didn't work.我尝试卸载并重新安装 Parcel,但它仍然不起作用。

I did get some success with npx parcel build src/index.js , but the behaviour seemed weird to me.我确实在npx parcel build src/index.js取得了一些成功,但这种行为对我来说似乎很奇怪。 It looked like it installed Parcel all over again and left a .parcel-cache folder in my directory.看起来它重新安装了 Parcel 并在我的目录中留下了一个 .parcel-cache 文件夹。 It did produce an output js but all it did to the js was change the one global variable to something with a long string of numbers in the name.它确实产生了一个输出 js,但它对 js 所做的只是将一个全局变量更改为名称中带有一长串数字的变量。 I just want to minify js.我只想缩小js。 After npx runs that command the terminal freezes. npx 运行该命令后,终端会冻结。 Is that normal?这是正常的吗?

I've copied your example and everything runs as expected.我已经复制了你的例子,一切都按预期运行。 You might want to remove the complete node_modules folder and npm i again.您可能想要再次删除完整的node_modules文件夹和npm i

Also, as a clean test, close your browsers.另外,作为一个干净的测试,关闭浏览器。 I'm not sure what your dev stack / environment is and there might be debuggers open in eg Chrome.我不确定您的开发堆栈/环境是什么,并且可能在 Chrome 中打开了调试器。

By the way, if parcel is not installed globally with the -g flag it's very possible the cli does not recognize the command.顺便说一句,如果没有使用-g标志全局安装parcel,则cli很可能无法识别该命令。 It's then only executable via script.然后它只能通过脚本执行。

It looks like you're having two problems - actually getting parcel installed correctly and getting unexpected output when you run it.看起来您遇到了两个问题 - 实际上正确安装了 Parcel 并在运行它时获得了意外的输出。 For the first problem, I can't repro it with the config you provided, and I don't have any ideas beyond what Jos Faber already suggested in his answer.对于第一个问题,我无法使用您提供的配置来重现它,除了 Jos Faber 在他的回答中已经提出的建议之外,我没有任何想法。

For the second problem - when you run npx parcel , you are expecting minified output, but don't get it.对于第二个问题 - 当您运行npx parcel ,您期望缩小输出,但没有得到它。 That's because of the "source" and "main" fields in your package.json.这是因为 package.json 中的"source""main"字段。 These are intended to be used by library authors - ie people creating npm packages that get consumed by other projects rather than sent to the browser.这些旨在供库作者使用 - 即人们创建的 npm 包被其他项目使用而不是发送到浏览器。 You (typically) wouldn't want to minimize your distributed library code because it would interfere with the ability of people who used it to debug, tree-shake, and minimize their apps, so parcel doesn't do this by default.您(通常)不想最小化您的分布式库代码,因为它会干扰使用它来调试、摇树和最小化应用程序的人的能力,因此默认情况下,parcel 不会这样做。 See parcel docs :查看包裹文档

By default, optimization is enabled during production builds (parcel build), except for library targets [emphasis added].默认情况下,在生产构建(包构建)期间启用优化,库目标除外[强调已添加]。

If you're developing a web app, you'll want to remove "source" and "main" from package.json and change your cli command to parcel build src/index.js (parcel will put the output in a dist folder by default, but if you want to change that, use the --dist-dir cli flag ).如果您正在开发 Web 应用程序,您需要从package.json删除"source""main" ,并将您的 cli 命令更改为parcel build src/index.js (parcel 会将输出放入dist文件夹中默认,但如果您想更改它,请使用--dist-dir cli 标志)。

On the off chance that you actually want to minify a library, you could do this by adding targets configuration to your package.json:如果您确实想缩小库,您可以通过将目标配置添加到 package.json 来做到这一点:

{
  ...
  "targets": {
    "main": {
      "optimize": true
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM