Webpack CLI 在项目中不起作用 [英] Webpack CLI does not work in project

查看:46
本文介绍了Webpack CLI 在项目中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照 React 教程的说明进行操作.我在终端中使用 npm install 安装了 NPM,并且能够启动节点服务器.问题是我无法运行 Webpack 以在浏览器中查看更新.

I have followed the instructions to a React tutorial. I installed NPM using npm install in the terminal and was able to start the node server. The issue is I am unable to run Webpack to view updates in my browser.

当我按照提示输入webpack --watch时,终端给出错误信息:'webpack' not found,但是node_modules中已经安装了Webpack.为什么会发生这种情况?

When I type webpack --watch as instructed, the terminal gives the error message: 'webpack' not found, but Webpack has been installed in node_modules. Why is this happening?

推荐答案

您必须知道本地安装和全局安装的区别,以及何时这样做.根据 NPM 文档:

You must know the difference of installing locally and globally, and when to do so. Per the NPM Documentation:

如果您想将其用作命令行工具,例如 grunt CLI,那么您需要全局安装它.另一方面,如果你想使用 Node 的 require 之类的东西依赖你自己模块中的包,那么你想在本地安装.(强调我的)

If you want to use it as a command line tool, something like the grunt CLI, then you want to install it globally. On the other hand, if you want to depend on the package from your own module using something like Node's require, then you want to install locally. (emphasis mine)

因此,您必须全局安装 Webpack 才能使用其 CLI(命令行界面).做:

Thus, you must install Webpack globally to use its CLI (command line interface). Do:

npm install webpack -g

这样,它是全局安装的,您可以使用 webpack 命令通过命令行运行 Webpack.如果你不全局安装,你仍然可以使用本地安装模块的路径并像这样执行:

That way, it's globally installed and you can run Webpack through the command line with the webpack command. If you don't install it globally, you can still use the path to the locally installed module and execute as such:

./node_modules/.bin/webpack --watch

这可能有点不方便,您应该选择全局安装.有关详细信息,请参阅Webpack 文档.

This might be a bit inconvenient and you should opt for the global install. See the Webpack Documentation for more details.

这篇关于Webpack CLI 在项目中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆