<commit_before>---
layout: post
title:  "Create our own module in Node.js"
date:   2014-01-10
categories: nodejs modules exports  
---

We have some functions that we want to be available through the `require` functionality in **node.js**. What does `require` returns? We use `require` when we want to reuse our code all throughtout our node.js application. Imagine we want to have a function which gives us a timestamp, after having a look at the [Javascript Date Object][dateobject] we create the function we want to use from any point in our app in our file `clock.js`: 

```javascript
//clock.js
var whatTimeIsIt = function(){
  console.log(new Date().toDateString());
}
exports = greetings;
```

As you can see, to define what we return when we *require* this function we have to use the `exports` object. Then in our application after *requiring* `clock.js` file, we can use it like this: 

```javascript
//app.js
var whatTimeIsIt = require('./clock');
whatTimeIsIt();
```

Another way is exporting a function into the `exports` object so when we write our require instruction later, we are going to get back and object (instead of a function), so we create export our function into the `exports` object like this:  

```javascript
//clock_object.js
exports.whatTimeIsIt = function(){
  console.log(new Date().toDateString());
}
```

And then, we require our file, we get back an object and then we can call the function we exported in that object: 

```javascript
//app.js
var object = require('./clock_object');
object.whatTimeIsIt();
```

Notice that there is no need to assign what is return by require to an object, we can call require and call the function that it returns directly:

```javascript
console.log(require('./clock_object').whatTimeIsIt();
```

Sources: Inspired by [Real Time Web Code School Course][realtimewebcodeschoolcourse]





<!-- [id_ref]: URL--> 
[dateobject]: http://www.w3schools.com/jsref/jsref_obj_date.asp
[realtimewebcodeschoolcourse]: https://www.codeschool.com/courses/real-time-web-with-nodejs<commit_msg>Update previous post. Correct some minor mistakes<commit_after>---
layout: post
title:  "Create our own module in Node.js"
date:   2014-01-10
categories: nodejs modules exports  
---

We have some functions that we want to be available through the `require` functionality in **node.js**. What does `require` returns? We use `require` when we want to reuse our code all throughtout our node.js application. Imagine we want to have a function which gives us a timestamp, after having a look at the [Javascript Date Object][dateobject] we create the function we want to use from any point in our app in our file `clock.js`: 

```javascript
//clock.js
var whatTimeIsIt = function(){
  console.log(new Date().toDateString());
}
exports = greetings;
```

As you can see, to define what we return when we *require* this function we have to use the `exports` object. Then in our application after *requiring* `clock.js` file, we can use it like this: 

```javascript
//app.js
var whatTimeIsIt = require('./clock');
whatTimeIsIt();
```

Another way is exporting a function into the `exports` object so when we write our require instruction later, we are going to get back an object (instead of a function), so we create export our function into the `exports` object like this:  

```javascript
//clock_object.js
exports.whatTimeIsIt = function(){
  console.log(new Date().toDateString());
}
```

And then, we require our file, we get back an object and then we can call the function we exported in that object: 

```javascript
//app.js
var object = require('./clock_object');
object.whatTimeIsIt();
```

Notice that there is no need to assign what is return by `require` to an object, we can call `require` and call the function that it returns directly:

```javascript
console.log(require('./clock_object').whatTimeIsIt();
```

Sources: Inspired by [Real Time Web Code School Course][realtimewebcodeschoolcourse]





<!-- [id_ref]: URL--> 
[dateobject]: http://www.w3schools.com/jsref/jsref_obj_date.asp
[realtimewebcodeschoolcourse]: https://www.codeschool.com/courses/real-time-web-with-nodejs
{'fraction_non_alphanumeric': 0.10611554266376601, 'fraction_numerical': 0.00435097897026831, 'mean_word_length': 4.546916890080429, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6604353', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1139, 'n_words': 529}