npm start使用MongoDB和mongoose抛出ELIFECYCLE错误

时间:2020-03-31 07:16:55

标签: node.js mongodb express npm

我一直在遵循有关设置MongoDB服务器的指南。但是,突然我无法通过npm start启动localhost:3000。它加载后,大约持续5-10秒,然后出现错误代码ELIFECYCLE。我尝试了别人提到的每个步骤,npm cache clean --force,npm install,删除package-lock.json文件,npm install之后,删除节点模块。似乎没有什么能解决我的问题。

这是我得到的调试日志:

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   'D:\\NodeJS\\node.exe',
1 verbose cli   'C:\\Users\\musta\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'start'
1 verbose cli ]
2 info using npm@6.14.4
3 info using node@v12.16.1
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle shopping-cart@0.0.0~prestart: shopping-cart@0.0.0
6 info lifecycle shopping-cart@0.0.0~start: shopping-cart@0.0.0
7 verbose lifecycle shopping-cart@0.0.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle shopping-cart@0.0.0~start: PATH: C:\Users\musta\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;D:\Other\Projects\Code\Powershell\shopping-cart\node_modules\.bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\dotnet\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;D:\NodeJS\;C:\Users\musta\AppData\Local\Microsoft\WindowsApps;C:\Users\musta\AppData\Roaming\npm
9 verbose lifecycle shopping-cart@0.0.0~start: CWD: D:\Other\Projects\Code\Powershell\shopping-cart
10 silly lifecycle shopping-cart@0.0.0~start: Args: [ '/d /s /c', 'node ./bin/www' ]
11 silly lifecycle shopping-cart@0.0.0~start: Returned: code: 1  signal: null
12 info lifecycle shopping-cart@0.0.0~start: Failed to exec start script
13 verbose stack Error: shopping-cart@0.0.0 start: `node ./bin/www`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Users\musta\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:311:20)
13 verbose stack     at ChildProcess.<anonymous> (C:\Users\musta\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:311:20)
13 verbose stack     at maybeClose (internal/child_process.js:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
14 verbose pkgid shopping-cart@0.0.0
15 verbose cwd D:\Other\Projects\Code\Powershell\shopping-cart
16 verbose Windows_NT 10.0.18363
17 verbose argv "D:\\NodeJS\\node.exe" "C:\\Users\\musta\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
18 verbose node v12.16.1
19 verbose npm  v6.14.4
20 error code ELIFECYCLE
21 error errno 1
22 error shopping-cart@0.0.0 start: `node ./bin/www`
22 error Exit status 1
23 error Failed at the shopping-cart@0.0.0 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

此外,我一直遵循的指南首先在index.js文件中使用了如下代码:

var express = require('express');
var router = express.Router();
var Product = require('D:/Other/Projects/Code/Powershell/shopping-cart/models/product');

/* GET home page. */
router.get('/', function(req, res, next) {
  Product.find(function(err, docs) {
    var productChunks = [];
    var chunkSize = 3;
    for (var i = 0; i < docs.length; i += chunkSize) {
           productChunks.push(docs.slice(i, i + chunkSize));
    }
    res.render('shop/index', { title: 'Shopping Cart', products: productChunks });
  });
});

但是,当我使用它时,localhost:3000页面会一直加载,直到出现与上述相同的错误为止,因此我一直在使用指南中之前使用的代码,直到npm start错误为止。工作代码为:

var express = require('express');
var router = express.Router();
var Product = require('D:/Other/Projects/Code/Powershell/shopping-cart/models/product');

/* GET home page. */
router.get('/', function(req, res, next) {
  var products = Product.find();
  res.render('shop/index.hbs', { title: 'Shopping Cart', products });
});


module.exports = router;

这些问题相关吗?我已经搜索了github和stackoverflow以及youtube指南中的评论,但似乎找不到问题。

我们将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:1)

因此,对于第二个问题,您只需要在根文件安装依赖项MiddleWares中使用body-parser并添加以下代码

//Middlewares for bodyParser
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());