How do I include functions from my other files in nodejs

0 votes

Let's say I have a file called app.js:

var express = require('express');
var app = express.createServer();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(req, res){
  res.render('index', {locals: {
    title: 'NowJS + Express Example'
  }});
});

app.listen(8080);

What if I have a functions inside "tools.js". How would I import them to use in apps.js?

Jul 9, 2020 in Node-js by kartik
• 37,520 points
3,315 views

1 answer to this question.

0 votes

Hello @kartik,

You require any js file,so you just need to declare what you want to expose.

// tools.js
// ========
module.exports = {
  foo: function () {
    // whatever
  },
  bar: function () {
    // whatever
  }
};

var zemba = function () {
}

And in your app file:

// app.js
// ======
var tools = require('./tools');
console.log(typeof tools.foo); // => 'function'
console.log(typeof tools.bar); // => 'function'
console.log(typeof tools.zemba); // => undefined

Hope it helps!!

answered Jul 9, 2020 by Niroj
• 82,880 points

Related Questions In Node-js

0 votes
1 answer

How can i export socket.io into other modules in nodejs?

Because app.js is usually the main initialization ...READ MORE

answered Jun 17, 2022 in Node-js by Neha
• 9,060 points
4,176 views
0 votes
1 answer

How to include route handlers in multiple files in Express?

Hello @kartik, If you want to put the routes ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,880 points
5,764 views
0 votes
1 answer

How do I add a custom script to my package.json file that runs a javascript file?

Hello @kartik, I have created the following, and ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,880 points
41,038 views
0 votes
1 answer

How do I update devDependencies in NPM?

Hello @kartik, To update package.json in addition to ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,880 points
7,373 views
0 votes
1 answer
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17, 2020 in Web Development by anonymous
• 37,520 points
853 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 1,024 views
+1 vote
1 answer

What are pseudo class in css??

Hey, The state of an element is controlled  by ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 852 views
0 votes
1 answer

How do I redirect in expressjs while passing some context?

Hello @kartik, The easiest way I have found ...READ MORE

answered Jul 14, 2020 in Node-js by Niroj
• 82,880 points
23,801 views
0 votes
1 answer

How do I URl encode something in Node.js?

Hello @kartik, The built-in module querystring is what you're looking ...READ MORE

answered Jul 14, 2020 in Node-js by Niroj
• 82,880 points
2,333 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP