JavaScript How to correctly import javascript function and use it into a node.js script?

so I have a time-stamp function in a javascript file that return a date that look like MM/DD/YY

I would like tom import what the function return into another script ( node.js) and display it whenever the script run.

But whenever I fire up the node.js program , I get something like : [object Object], and I have no idea where this comes from ...

Here is the timeStamp.js

function timeStamp() {
    let now = new Date();
    let date = [ now.getMonth() + 1, now.getDate(), now.getFullYear() ];
    let time = [ now.getHours(), now.getMinutes(), now.getSeconds() ];
    let suffix = ( time[0] < 12 ) ? "AM" : "PM";
    time[0] = ( time[0] < 12 ) ? time[0] : time[0] - 12;
    time[0] = time[0] || 12;
    for ( var i = 1; i < 3; i++ ) {
        if ( time[i] < 10 ) {
            time[i] = "0" + time[i];
        }
    }
    return date.join("/") + " " + time.join(":") + " " + suffix;
}

and here is the node.js script

let io = require('socket.io').listen(process.env.port||5000);

var date = require('./timeStamp');

io.on('connection', function(socket) {

    console.log('Date is ...'+date);

    socket.on('data',function (data , callback) {
        console.log(`"${data}" was received ...`);
        callback(true);
    });
});

How can I fix this bug or what am I doing wrong or missing ?

Answer:1

You're missing: module.exports = timeStamp; without it, when using require an empty object will be exported, that's why you get [Object object]

console.log('Date is...' + {});
Answer:2

You need to add timeStamp function to exports object then you'll be able to require it in any file you want. And this is how you do that

module.exports = timeStamp;

in your timeStamp.js file.

And this is how you'll call that function in your node script

var date = require('./timeStamp');
date();
Answer:3

I need to merge a Map instance w/ an object. This didn't work: > map = new Map([ ['foo', 1] ]) Map { 'foo' => 1 } > Object.assign({}, map) {} Looks like Object.assign doesn't like maps. Is ...

I need to merge a Map instance w/ an object. This didn't work: > map = new Map([ ['foo', 1] ]) Map { 'foo' => 1 } > Object.assign({}, map) {} Looks like Object.assign doesn't like maps. Is ...

I have JSON array that looks like var data = { "fields": [{ "firstName": { "fieldName": "First Name", "required": true, "provided": false } }, { "...

I have JSON array that looks like var data = { "fields": [{ "firstName": { "fieldName": "First Name", "required": true, "provided": false } }, { "...

I have lodash cdn included in this fiddle https://jsfiddle.net/byhh6Lyo/ console.log(_) console.log(_.range(1, 10)) _.map([1,2,3], (o) => console.log(o)) //worked The second line has an error, ...

I have lodash cdn included in this fiddle https://jsfiddle.net/byhh6Lyo/ console.log(_) console.log(_.range(1, 10)) _.map([1,2,3], (o) => console.log(o)) //worked The second line has an error, ...

  1. lodash range function

I have this PHP array ($savedRequestDates) of dates: Array ( [0] => 2018-03-29 10:56:31 [1] => 2018-03-29 10:58:09 [2] => 2018-04-12 11:28:41 [3] => 2018-04-12 13:07:25 [4] => 2018-05-...

I have this PHP array ($savedRequestDates) of dates: Array ( [0] => 2018-03-29 10:56:31 [1] => 2018-03-29 10:58:09 [2] => 2018-04-12 11:28:41 [3] => 2018-04-12 13:07:25 [4] => 2018-05-...