I'm trying to initialize a simple express app with the following code:
const express = require('express');
const app = express()
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const mongoose = require('mongoose');
port = 3000
//creating the db
mongoose.connect('mongodb://localhost:27017/Criptare',{ useNewUrlParser: true,
useCreateIndex: true });
let db = mongoose.connection
db.on('error',console.error.bind(console,"connection error"));
//parsers
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//session LATER
//static files
app.use('/static',express.static('public'));
//view engine PUG
app.use('view-engine','pug');
//Routes
const mainRoutes = require('./routes/main')
app.use(mainRoutes)
//error handle
app.use((req,res,next)=>{
let err = new Error('Looks like the page you were looking for was not found')
err.status = 404;
next(err)
})
app.use((err,req,res,next)=>{
res.locals.error = err
res.status(err.status);
res.render('error')
});
app.listen(port,()=>{
console.log(`No bun serveru ii on pe ip-ul:localhost:${port}`)
})
After I run it with nodemon I get the following error:
module.js:550
throw err;
^
Cannot find module 'C:\Proiecte code\ON\express\criptare\index.js'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
Anyone has any idea why I'm getting this? I have all the dependencies inside the package.json
Change directory to where your filename.js is located then,run node filename.js
Initialize npm in the current folder where the script is stored.
I was getting the same issue. Running "yarn" fixed the issue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.