第一个node.js项目在连接mongodb时遇到问题



这是我使用node.js的第一个项目,我正处于将node.js与mongoose和MongoDB连接起来的阶段。

我刚刚从mongodb安装了api,并键入";npm运行devstar";t,这就是我得到的错误。。。

"PS C:\Users\ryant\Desktop\letstravel>npm运行devstart

lets-travel@0.0.0devstart C:\Users\ryant\Desktop\letstravelnodemon/bin/www-

[节点]2.0.4[节点]要随时重新启动,请输入rs[节点]监视路径:[nodemon]监视扩展:js、mjs、json
[nodemon]启动node ./bin/wwwC: \Users\ryant\Desktop\letstravel\bin\www:1npm/usr/bin/env^

ReferenceError:npm未定义

**at Object.<anonymous> (C:UsersryantDesktoplets-travelbinwww:1:1)**    (this is the only line that is highlighted the rest are greyed out)
at Module._compile (internal/modules/cjs/loader.js:1251:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
at Module.load (internal/modules/cjs/loader.js:1100:32)
at Function.Module._load (internal/modules/cjs/loader.js:962:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47

[节点]应用程序崩溃-在启动之前等待文件更改">

我猜这与api安装有关,因为我不能100%确定。

我知道这可能是一个愚蠢的问题,但社区有可能列出一些我应该看看的事情吗?

谢谢大家抽出时间。

这是我的包.json

{
"name": "lets-travel",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"devstart": "nodemon ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"mongodb": "^3.6.0",
"mongoose": "^5.10.2",
"morgan": "~1.9.1",
"pug": "2.0.0-beta11"
},
"devDependencies": {
"nodemon": "^2.0.4"
}
}

这是我的app.js(去掉了api用户名和密码(

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const mongoose = require('mongoose');
var indexRouter = require('./routes/index');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
//Set up mongoose connection
mongoose.connect('mongodb:/@cluster0-shard-00-00.qlpe0.mongodb.net:27017,cluster0-shard-00-01.qlpe0.mongodb.net:27017,cluster0-shard-00-02.qlpe0.mongodb.net:27017/database?ssl=true&replicaSet=atlas-syg3pr-shard-0&authSource=admin&retryWrites=true&w=majority');
mongoose.Promise = global.Promise;
mongoose.connection.on('error', (error) => console.error(error.message) );
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;

我可以根据要求在评论中添加任何其他内容。

正如错误所说,npm没有定义——看起来要么你没有安装npm/nodejs,要么你没有在env中设置路径。请在terminal/cmd中运行npm -v以检查是否相同。

此外,只是一个快速的建议,不要在任何论坛上粘贴你的连接字符串也直接在代码中。将其保存在配置文件或环境变量中

最新更新