webpack 命令不起作用 [英] webpack command not working
问题描述
我是 Node Js 和 Webpack 的新手.我尝试使用模块加载器启动一个项目.
I am new to Node Js and Webpack. I tried to start a project with module-loaders.
首先,我安装了 nodeJs 和 NPM,并创建了一个名为 tutorial
的新目录.我使用命令提示符 cd 进入该目录,然后运行以下命令 npm init
然后使用以下命令通过 npm
安装 webpack:
Firstly, I installed nodeJs and NPM and created a new directory called tutorial
. I used the command prompt to cd into this directory and then ran the following command npm init
and then installed webpack via npm
using the command below :
npm install -S webpack
第一个命令将 webpack 本地安装到node-modules"目录下的项目中,我可以通过执行以下操作来运行我的项目:
The 1st command installed webpack locally into the project under the 'node-modules' directory and I can run my project by doing this:
nodejs node-modules/webpack/bin/webpack.js
这个问题是我必须把我的 webpack.config.js
文件放在这个目录中,我想把它放在我的项目根目录中.
The problem with this is that I have to place my webpack.config.js
file inside of this directory which I want to place in my project root.
这个问题的一个解决方案是在我的机器上全局安装 webpack,我使用下面的命令:
One solution to this problem was to install webpack globally on my machine which I did using the command below :
npm install -g webpack
这安装了 Webpack,现在我有了一个 Webpack 命令.但是,此命令似乎根本不起作用或做任何事情.当我尝试从我的项目的根目录运行它时,它根本不执行任何操作(请参阅屏幕截图)
This installed Webpack and now I do have a Webpack command. However, this command does not seem to be working or doing anything at all. When I try to run this from my project's root directroy it does not do anything at all (See Screenshot)
请告诉我我做错了什么!!
Please tell me what I am doing wrong!!
推荐答案
webpack
不仅在您的 node-modules/webpack/bin/
目录中,它还链接在 node_modules/.bin
中.
webpack
is not only in your node-modules/webpack/bin/
directory, it's also linked in node_modules/.bin
.
您可以使用 npm bin
命令来获取 npm 将安装可执行文件的文件夹.
You have the npm bin
command to get the folder where npm will install executables.
您可以使用 package.json
的 scripts
属性来使用将导出的该目录中的 webpack.
You can use the scripts
property of your package.json
to use webpack from this directory which will be exported.
"scripts": {
"scriptName": "webpack --config etc..."
}
例如:
"scripts": {
"build": "webpack --config webpack.config.js"
}
然后你可以运行它:
npm run build
甚至有参数:
npm run build -- <args>
这允许您将 webpack.config.js
放在项目的根文件夹中,而无需全局安装 webpack 或将 webpack 配置放在 node_modules
文件夹中.
This allow you to have you webpack.config.js
in the root folder of your project without having webpack globally installed or having your webpack configuration in the node_modules
folder.
这篇关于webpack 命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!