Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call .js from another .js file? [duplicate]

I have mysql connection code which I need to call each time in every .js file. Say I want sql.js from main.js. I am thinking include(sql.js) ?

 sql.js

 var sql = require('sql');
 var connection = sql.createConnection({
 host : 'localhost',
 user : 'root',
 password : '',
 database : 'db'
 });

connection.connect(function(err){
if(!err) {
console.log("connected");
}
like image 704
jeny Avatar asked Mar 16 '16 23:03

jeny


1 Answers

You can create a module, and require it the following way.

File A: sql.js

var a = function a(){

};

module.exports.a = a;

Files B, C, D:

var sql = require("./sql");

sql.a();
like image 62
Pavel Avatar answered Oct 22 '22 19:10

Pavel