Nodejs安装karma 出现 Cannot find module 'optimist'错误

http://www.javashuo.com/article/p-pnrbedet-dc.html

安装好nodejs、配置好cnpm、以及自动化测试所需的karma-cli、karma和jasmine以后node

$ npm install -g karma-cli

$ cnpm install karma --save-dev
√ Installed 1 packages
√ Linked 0 latest versions
√ Run 0 scripts
√ All packages installed (used 172ms, speed 110.67kB/s, json 1(19.04kB), tarball 0B)

$ npm install karma-jasmine karma-chrome-launcher --save-dev
npm WARN karma-jasmine@0.2.3 requires a peer of karma@>=0.9 but none is installed. You must install peer dependencies yourself.
npm WARN ajs-up-running-chapter-3@0.0.1 No description
npm WARN ajs-up-running-chapter-3@0.0.1 No repository field.
npm WARN ajs-up-running-chapter-3@0.0.1 No license field.

+ karma-jasmine@0.2.3
+ karma-chrome-launcher@0.1.12
removed 422 packages and updated 2 packages in 5.129s

运行出错chrome

$ karma start
module.js:538
    throw err;
    ^

Error: Cannot find module 'optimist'
    at Function.Module._resolveFilename (module.js:536:15)
    at Function.Module._load (module.js:466:25)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\von\path\to\work\node_modules\_karma@1.7.1@karma\lib\cli.js:2:16)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)

解决:npm

$ cat package.json
{
  "name": "myname",
  "version": "0.0.1",
  "devDependencies": {
    "karma": "^1.7.1",
    "karma-chrome-launcher": "^0.1.12",
    "karma-jasmine": "^0.2.3"
  }
}

$ rm node_modules/ -rf
$ npm cache clean --force
$ cnpm install
platform unsupported karma@1.7.1 › chokidar@1.7.0 › fsevents@^1.0.0 Package require os(darwin) not compatible with your platform(win32)
[fsevents@^1.0.0] optional install error: Package require os(darwin) not compatible with your platform(win32)
√ Installed 3 packages
√ Linked 156 latest versions
√ Run 0 scripts
anti semver karma@1.7.1 › useragent@2.2.1 › tmp@0.0.x delcares tmp@0.0.x(resolved as 0.0.33) but using ancestor(karma)'s dependency tmp@0.0.31(resolved as 0.0.31)
√ All packages installed (175 packages installed from npm registry, used 5s, speed 58.01kB/s, json 159(311.04kB), tarball 0B)
posted @ 2022-09-02 08:55  脚踏实地&积少成多  阅读(162)  评论(0编辑  收藏  举报