为什么包装脚本的行为与相同的终端命令有所不同

发布于 2025-02-13 05:42:24 字数 494 浏览 2 评论 0原文

在我的npm项目中,在我的package.json文件中,我有以下代码行:

  "scripts": {
    "build": "webpack"
  },

在终端中,如果我运行npm webpack,我收到错误消息:

Unknown command: "webpack"

但是,如果我运行npm run build,我从webpack中得到一个提示,说我需要webpack-cli ...因此,显然可以识别命令。

我对这两个命令的不同行为感到困惑。在这种情况下,运行npm run build与我的终端中的npm webpack相同吗?为什么一个命令失败并成功?当我运行npm run build时,实际发生了什么?

In my npm project, in my package.json file, I have the following lines of code:

  "scripts": {
    "build": "webpack"
  },

While in my terminal, if I run npm webpack, I get the error message:

Unknown command: "webpack"

But if I run npm run build, I get a prompt from webpack saying I need webpack-cli... so the command is obviously recognized.

I'm confused about the different behavior of these two commands. In this case, isn't running npm run build identical to running npm webpack in my terminal? Why does one command fail and one succeed? What is actually happening when I run npm run build?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2025-02-20 05:42:25

如果我们查看 documentation>,

环境

软件包脚本在一个环境中运行,其中提供了许多有关NPM设置和流程当前状态的信息。

路径

如果您依赖定义可执行脚本(例如测试套件)的模块,则将这些可执行文件添加到执行脚本的路径中。

也许这就是命令行识别的原因webpack

If we look at the documentation,

Environment

Package scripts run in an environment where many pieces of information are made available regarding the setup of npm and the current state of the process.

path

If you depend on modules that define executable scripts, like test suites, then those executables will be added to the PATH for executing the scripts.

Maybe this is the reason webpack is not recognized by the command line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文