NPM 启动时显示找不到模块 'safe-buffer'

NPM while starting it shows Cannot find module 'safe-buffer'

我正在尝试创建一个节点 Restful API 服务,但是 npm 在启动时给我一个错误。在命令提示符下,我输入了以下内容:

D:\xampp\htdocs\todoListApi>npm run start

todolistapi@1.0.0 start D:\xampp\htdocs\todoListApi

nodemon server.js

显示以下错误

[nodemon] 1.17.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
internal/modules/cjs/loader.js:550
    throw err;
    ^

Error: Cannot find module 'safe-buffer'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:548:15)
at Function.Module._load (internal/modules/cjs/loader.js:475:25)
at Module.require (internal/modules/cjs/loader.js:598:17)
at require (internal/modules/cjs/helpers.js:11:18)


     at Object.<anonymous> (D:\xampp\htdocs\todoListApi\node_modules\readable-str
    eam\lib\_stream_readable.js:55:14)

    at Module._compile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! todolistapi@1.0.0 start: `nodemon server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the todolistapi@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional log
ging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\admin\AppData\Roaming\npm-cache\_logs18-04-20T06_50_15_
033Z-debug.log

D:\xampp\htdocs\todoListApi>npm install mongoose --sav
npm WARN todolistapi@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules\fse
vents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@
1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}

相反,它应该启动 npm 并将结果显示为

todo list RESTful API server started on: 3000

请指导我如何克服这个问题。

以下步骤对我有用:

  • 从控制面板卸载 Node.js
  • 去C:/Users/Admin/AppData/Roaming
  • 手动删除 npmnpm-cache

再次尝试安装 node.js。

尝试使用 npm cache clean --force,如果仍然无效,请尝试完全重新安装 NPM/Node :)