2

I defined many functions in a file called util.js, and I will take the function encode as an example. when using, it should be like this:

var util = require('./util.js');
util.encode();

but I want to use encode directly,just like using it in PHP or Python:

include "util.php"
encode();

Is there any way to achieve in NodeJS? Thanks.

1

2 Answers 2

1

Yes, you can do it in this way:

var encode = require('./util.js').encode;
encode();
Sign up to request clarification or add additional context in comments.

Comments

1
//utils.js
decode = function(){
    console.log(222);
}
exports.wrap = function(g){
    g.encode = function(){}
}

//main.js
require('./utils.js').wrap(global);
encode();
decode();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.