Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:51:37+00:00 2026-06-11T07:51:37+00:00

First off, I am a totally new developer with regards to Node.js. I have

  • 0

First off, I am a totally new developer with regards to Node.js.

I have started creating a sample express application, and I wanted to use an additional module to learn more about it. I have installed via npm the “mysql” module, all fine.

I have added it at the beginning of app.’s, like this:

var mysql = require('mysql');

now, as you already know, express created an index.js file inside the directory routes: i would like to be able to access the mysql variable to connect to the db from this index.js page but, using the command

var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'root',
});

obviously doesn’t work, giving the “500 ReferenceError: mysql is not defined”.

Of course, I am sure I need to pass this variable, but I really have no clue, can any good soul enlighten me? I know this is a very small and basic thing, but I tried this already, and doesn’t seem to work:

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

and on index.js:

exports.index = function(req, res, mysql){ ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:51:39+00:00Added an answer on June 11, 2026 at 7:51 am

    In Node.js, you should require modules in the files you need to use them in; so, if you want to use the mysql package in your routes/index.js file, at the top of that file, do

    var mysql = require('mysql');
    

    You may end up requiring a module in more than one file; this is normal and, in many ways, a good thing (no namespacing issues, etc.)

    If you want to learn more about modules and packages, you may be interested in these two screencasts:

    • Modules: http://nodecasts.net/episodes/3-modules
    • Packages & npm: http://nodecasts.net/episodes/4-packages-and-npm
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, let me start by saying that I am totally new to working
First off, I am totally new to PHP, but so far i love the
First off I have gone through this link http://codex.wordpress.org/Integrating_WordPress_with_Your_Website . I cannot use this.
First off, I would like to make clear, that I am SUPER NEW TO
First off I use this code to make the navigation bar always stay fixed;
First off, I've never used GWT before. I have good experience in HTML/CSS/JS/JSP. I'm
First off, a big thanks to the people behind the new boost::geometry library! This
First off, I'm pretty new so I hope I hadn't missed anything too trivial.
First off, please accept my apologies if this question is basic, I mainly have
First off I have an app that provides the user information about the amount

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.