Meaning of "This" in Node.Js Modules and Functions

Meaning of this in node.js modules and functions

Here's a few fundamental facts you must understand to clarify the situation:

  • In the top-level code in a Node module, this is equivalent to module.exports. That's the empty object you see.

  • When you use this inside of a function, the value of this is determined anew before each and every execution of the function, and its value is determined by how the function is executed. This means that two invocations of the exact same function object could have different this values if the invocation mechanisms are different (e.g. aFunction() vs. aFunction.call(newThis) vs. emitter.addEventListener("someEvent", aFunction);, etc.) In your case, aFunction() in non-strict mode runs the function with this set to the global object.

  • When JavaScript files are required as Node modules, the Node engine runs the module code inside of a wrapper function. That module-wrapping function is invoked with a this set to module.exports. (Recall, above, a function may be run with an abitrary this value.)

Thus, you get different this values because each this resides inside a different function: the first is inside of the Node-created module-wrapper function and the second is inside of aFunction.

What does this mean in a nodejs module?

this (in the context of a module) is the same as exports in node.js. However you should generally use exports/module.exports instead, so that it's explicitly clear what you're modifying.

Node.js: What is the context of the `this` operator when used in module scope?

Inside a Node module, this by design refers to module's exports object:

console.log(this === exports); // true

Making console.log(this.foo) equivalent to console.log(exports.foo).

In other words, neither does this refer to the global object nor do local variables magically become properties of exports.

Since exports.foo doesn't exist, you get undefined.

NODE.JS Functions declared in module can access variable but same function imported from other module cannot

In NodeJS, all files are modules. A variable you define in one file isn't readily accessible to another file just because it was imported first.

If you're coming from using script tags on the client side, it may seem like all you have to do is put the scripts in the right order (or import modules in the right order) in order to get access to different pieces of code. However, in NodeJS, each file is its own entity. It doesn't just turn into one long, global piece of JavaScript like it does in the browser.

If you want code from one file in NodeJS to work in another file, you have to define what you want to export from that file and make sure you're importing it into the file you'd like to use it in. In your example, arr[0] doesn't exist in test2.js. That's why you're getting an error that arr is not defined. If you want to use arr in the function you're importing from test2.js, you can do the following:

/*  test2.js  */
module.exports = (x) => console.log(x)

/* main file */
const arr = [1, 2, 3]
const foo = require("./test2")

foo(arr[0])
// > 1

How to see the whole definitions: functions and variables from node modules in Angular 7?

You will need a type definition for that javascript module to be able to do that.
Have a look at : The repository for high quality TypeScript type definitions

meaning of module.exports= function in node.js

It works exactly the same as the first one does, only that it exports a function instead of an object.

The module that imports the module can then call that function:

var dummy = require('./dummy.js');
dummy();

any idea how can i export variables from module defined like this..?

Since functions are just objects, you can also assign properties to it:

module.exports = function(passport,config, mongoose) {}
module.exports.user = 'rally';

However I'd argue that this is less expected if a module directly exports a function. You are probably better off exporting the function as its own export:

exports.login = function(passport,config, mongoose) {}
exports.user = 'rally';

Functions in Node.js modules

exports is a special object which is included in every JS file in the Node.js application by default.

So anything you export in a file (mymodule.js in your case) is a property on this object and when you require this module export object is assigned to the requiring object (baz in your case).

When you do

var baz = require("./mymodule.js");

Your baz variable now looks like something

baz = {
bar : "Hello",
foo : () => {
console.log(baz.bar)
}
}

And when you call baz.foo() you see "Hello" printed



Related Topics



Leave a reply



Submit