命令'npm run dev'不起作用,我该怎么办?



我正在 cmd 中尝试 npm 运行 dev,它抛出了一个错误,我已经尝试了很多东西,但它似乎不起作用。

这是我的package.json文件:

{
"name": "project",
"version": "1.0.0",
"description": "project",
"main": "index.js",
"scripts": {
"dev": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"babel-polyfill": "^6.26.0"
}
}

这是我的wepack.config.js:

const path = require('path');

module.export = {
entry: './src/js/index.js',
output: {
path: path.resolve(__dirname, 'dist/js'),
filename: 'bundle.js'
},
mode: 'development'
};

我更新了我的 npm,尝试了 npm 缓存清理 - 强制但还有许多其他事情,但似乎没有任何效果。请帮忙。

这是我得到的错误:

Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.
Hash: c1735700111314ac598b
Version: webpack 4.43.0
Time: 62ms
Built at: 05/25/2020 1:24:29 PM
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
ERROR in Entry module not found: Error: Can't resolve './src' in 'C:UsersusmanDesktopcodingForkify'
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! forkify@1.0.0 dev: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the forkify@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     C:UsersusmanAppDataRoamingnpm-cache_logs2020-05-25T08_24_29_792Z-debug.log.

我以管理员身份使用 PowerShell 卸载了 webpack cli,并使用 PowerShell 安装了它,它对我来说非常有效。这是一个管理问题,因此请确保使用 PowerShell 下载 cli。这些是我使用的命令:

npm uninstall webpack webpack-cli

对于安装:

npm install webpack webpack-cli --save-dev  

我希望我能有所帮助。谢谢

您可以尝试更改您的 webpack.config.js如下所示:

const path = require("path");
module.exports = {
entry: "./src/js/index.js",
output: {
path: path.resolve(__dirname, "dist/js"),
filename: "bundle.js",
},
devServer: {
contentBase: path.resolve(__dirname, "dist"),
publicPath: "/js/",
},
};

并修改 package.json 文件中的脚本对象,如下所示:

"scripts": {
"serve": "webpack-dev-server --mode development"

},

最后运行这个命令:npm run serve

相关内容

最新更新