npm run server报错原因之一:Cannot find module 'webpack-cli/bin/config-yargs'

使用npm run server 启动webpack-dev-server时报错:

E:\webpack_demo>npm run server

> webpacktest@1.0.0 server E:\webpack_demo
> webpack-dev-server

The CLI moved into a separate package: webpack-cli.
Please install 'webpack-cli' in addition to webpack itself to use the CLI.
-> When using npm: npm install webpack-cli -D
-> When using yarn: yarn add webpack-cli -D
module.js:529
    throw err;
    ^

Error: Cannot find module 'webpack-cli/bin/config-yargs'
    at Function.Module._resolveFilename (module.js:527:15)
    at Function.Module._load (module.js:476:23)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (E:\webpack_demo\node_modules\webpack-dev-server\bin\webpack-dev-server.js:65:1)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! webpacktest@1.0.0 server: `webpack-dev-server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the webpacktest@1.0.0 server 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:\Users\wagon\AppData\Roaming\npm-cache\_logs\2018-03-28T07_39_08_944Z-debug.log

原因是找不到webpack-cli这个包,咱们使用npm添加此包即可:

E:\webpack_demo>npm install webpack-cli
npm WARN deprecated nomnom@1.8.1: Package no longer supported. Contact support@npmjs.com for more info.
npm WARN deprecated babel-preset-es2015@6.24.1: ????  Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
npm WARN webpacktest@1.0.0 No repository field.
npm WARN webpacktest@1.0.0 scripts['server'] should probably be scripts['start'].
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ webpack-cli@2.0.13
added 333 packages in 76.978s

然后再执行npm run server:

E:\webpack_demo>npm run server

> webpacktest@1.0.0 server E:\webpack_demo
> webpack-dev-server

i 「wds」: Project is running at http://192.168.3.6:1717/
i 「wds」: webpack output is served from /
i 「wds」: Content not from webpack is served from E:\webpack_demo\dist
‼ 「wdm」: Hash: a9eea546fbe29f1fcd52
Version: webpack 4.2.0
Time: 6349ms
Built at: 2018-3-28 15:47:09
    Asset     Size  Chunks             Chunk Names
entry2.js  139 KiB       0  [emitted]  entry2
 entry.js  139 KiB       1  [emitted]  entry
Entrypoint entry = entry.js
Entrypoint entry2 = entry2.js
   [3] (webpack)/hot/emitter.js 77 bytes {0} {1} [built]
   [4] (webpack)/hot/log.js 1.03 KiB {0} {1} [optional] [built]
   [5] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {0} {1} [built]
   [8] ./node_modules/html-entities/index.js 231 bytes {0} {1} [built]
  [10] (webpack)-dev-server/client/overlay.js 3.58 KiB {0} {1} [built]
  [12] (webpack)-dev-server/client/socket.js 1.05 KiB {0} {1} [built]
  [13] ./node_modules/loglevel/lib/loglevel.js 7.68 KiB {0} {1} [built]
  [14] ./node_modules/ansi-regex/index.js 135 bytes {0} {1} [built]
  [15] ./node_modules/strip-ansi/index.js 161 bytes {0} {1} [built]
  [22] ./node_modules/url/url.js 22.8 KiB {0} {1} [built]
  [23] (webpack)-dev-server/client?http://192.168.3.6:1717 7.75 KiB {0} {1} [built]
  [24] ./src/entry2.js 16 bytes {0} [built]
  [25] multi (webpack)-dev-server/client?http://192.168.3.6:1717 ./src/entry2.js 40 bytes {0} [built]
  [26] ./src/entry.js 58 bytes {1} [built]
  [27] multi (webpack)-dev-server/client?http://192.168.3.6:1717 ./src/entry.js 40 bytes {1} [built]
    + 13 hidden modules

WARNING in configuration
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment.
i 「wdm」: Compiled with warnings.

OK,搞定了 。下面给大家献上我的webpack配置和package.json:

这个是package.json:

{
  "name": "webpacktest",
  "version": "1.0.0",
  "description": "test webpack",
  "main": "index.js",
  "scripts": {
    "server": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^4.2.0",
    "webpack-dev-server": "^3.1.1"
  },
  "dependencies": {
    "live-server": "^1.2.0",
    "webpack-cli": "^2.0.13"
  }
}

这个个是webpack.config.js:

const path = require('path');
module.exports = {
    entry: {
        entry: './src/entry.js',
        entry2: './src/entry2.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    },
    module: {},
    plugins: [],
    devServer: {
        contentBase: path.resolve(__dirname, 'dist'),
        host: '192.168.3.6',
        compress: true,//服务器压缩
        port: 1717
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,802评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,109评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,683评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,458评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,452评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,505评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,901评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,550评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,763评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,556评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,629评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,330评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,898评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,897评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,140评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,807评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,339评论 2 342

推荐阅读更多精彩内容