Adding external javascript files to node.js

I have a node server and I want to add an external .js file (let's say something.js). I have this code:

var st = require('./js/something');

Where something.jsis the javascript file inside the folder /js/. The server compiles and starts, but when I try to use the functions defined in something.jsnode, it reports that they are not defined.

I also tried to run them using st.s(), but nothing happens, and I have an error saying that the object has no method s().

Can anybody help me?

Thank,

EDIT:

logging stgives {}(I get it from console.log(JSON.stringify(st)). Also execution console.log(st)gives the result {}.

Content something.jsis simply a collection of functions defined as

function s() {
    alert("s");
}

function t() {
    alert("t");
}
+5
1

Node.js CommonJS. , exports, . , ,

var st = require('./js/something');
st.s();
st.t();

. .

exports.s = function () {
    console.log("s");
}

exports.t = function () {
    console.log("t");
}
+8

All Articles