Error in Node.JS

Asked

Viewed 8,975 times

0

Guys I’m following a post to start seeing Node.JS and Angular.

Apparently everything was going well until the moment I’m going to do the first test, which, according to post, I have to give the command "Node bin www" on CMD. But when I give this command it presents the following message:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'http-errors'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (C:\Users\thaci\bau-node-mysql\bau-online\app.js:1:19)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

After that the post says that I just go in my browser and put 'localhost:3000' and would appear to the data entered in my Mysql table.

The post asked me to move the file 'app.js':

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var mysql = require('mysql'),
    myConnection = require('express-myconnection');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger('dev'));

// It has to be registered somewhere before app.router
app.use(myConnection(mysql, {
    host: 'localhost',
    user: 'root',
    password: 'thaci@11',
    port: 3306,
    database: 'devmedia_bau'
}, 'request'));

app.use(app.router);

app.get('/', routes.index);

http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});

And also asked to touch the file 'index.js':

var express = require('express');
var router = express.Router();

/*
 * GET home page.
 */

 router.get('/musicas', function(req, res){
     req.getConnection(function(err, connection){
         connection.query('SELECT * FROM tb_musicas', [], function(err, result){
             if(err){
                 return res.status(400).json(err);
             }
             return res.status(200).json(result);
         });
     });
 });

module.exports = router;

exports.index = function (req, res, next) {

    req.getConnection(function (err, connection) {
        connection.query('SELECT ? AS RESULT', ['Hello World!'], function (err, results) {
            if (err) return next(err);

            res.render('index', {
                title: 'express-myconnection',
                result: results[0].RESULT
            });
        });
    });

};

No post says nothing about, I found nothing on the internet and I do not know if it is something wrong in the code or if I have to tinker with some Node settings. Thanks in advance for any help! Thank you!

1 answer

0


Friend by what is described in his error, he is not finding the module http-errors.
This module must be present inside the folder node_modules which is usually at the root of the project, along with the app.js file.
If you don’t even have the node_modules folder or if it is empty it must be because you forgot to give the command npm install express, it will install all the dependencies needed for you to run your project.
NOTE: You must execute the command npm install express by prompt/shell at the root of your project.

  • Oh boy, it was that same but funny post does not even talk about this command kkkkk

  • Vixe guy, if the post you are following does not speak I think it would be better if you follow another tutorial saw rsrsrs, maybe this post you are following should be considering that readers already have more experience with the nodejs.

  • Yeah, I’ll see another but I think the most boring part for me is the connection to the bank that is now successful rest maybe no more need tutorial

Browser other questions tagged

You are not signed in. Login or sign up in order to post.