-2

I have two modules one written in ts the other one in js. One of the utilities in js module need to be accessed in ts module.

So utility service.js as follows,

module.exports = {
    helloFriends: function (message) {
        console.log(message);
    }
}

console.log('This part should not get invoked');

The called caller.ts as follows,

import { helloFriends } from './../moduleb/service';


helloFriends('Hello');

The output of above tsc caller.ts then node caller.js outputs following,

This part should not get invoked
Hello

I don't want any other code to get invoked from service.js except the function helloFriends, What can I do?

Note: Both modules are separate from each other with their own node dependencies.

Update:1

I handled with a hack, I defined IAM in both the modules .env files.

For service.js .env has IAM=service, For caller.ts .env has IAM=caller,

So if service.js is called from its own module IAM is service but when it is called from outside e.g. from caller.ts it has IAM value as caller then in service.js I made following changes:

In service.js I made changes as follows:

var iam = process.env.IAM;
module.exports = {
        helloFriends: function (message) {
            console.log(message);
        }
    }
    
if (iam === 'service') {
    console.log('This part should not get invoked, When called by external modules other than service');
}

So based on caller configuration I decide whether to execute a particular code section or not.

The plugin used for .env https://www.npmjs.com/package/dotenv

Update:2

Based on learnings I had after this question better approach would be to have functions to serve each purpose/functionality & export them individually.

Prashant
  • 4,474
  • 8
  • 34
  • 82
  • While downvoting adding comment will help, Thanks – Prashant Aug 06 '19 at 06:59
  • 1
    Not my downvote, but what have you tried already? Can't you just remove the code, or place it in a method that is exported? – Caramiriel Aug 06 '19 at 07:06
  • 1
    Didn't downvote, but AFAIK this isn't possible. I believe `require`/`import` will run through the contents of the file which includes execution of any function calls. See here: https://stackoverflow.com/a/37325716/3813411 – rb612 Aug 06 '19 at 07:07
  • Caramiriel & rb612 thanks for suggestions it helped me to deduce the answer. – Prashant Aug 06 '19 at 10:36

2 Answers2

0

I think the problem here is hinted at by this question: How does require() in node.js work?. In the question the asker has the following code in a module:

// mod.js
var a = 1;
this.b = 2;
exports.c = 3;

When mod.js is imported, the following properties are set:

// test.js
var mod = require('./mod.js');
console.log(mod.a);    // undefined
console.log(mod.b);    // 2
console.log(mod.c);    // 3

The this/exports difference is interesting by itself, but it also tells us that the whole module is being run when it is required/imported and as it is stated in the answer you can even return part way through the module code.

This means that your console.log('This part should not get invoked'); will unfortunately be invoked unless you have a way of exiting the module code

ngood97
  • 513
  • 5
  • 16
0

Update:1

I handled with a hack, I defined IAM in both the modules .env files.

For service.js .env has IAM=service, For caller.ts .env has IAM=caller,

So if service.js is called from its own module IAM is service but when it is called from outside e.g. from caller.ts it has IAM value as caller then in service.js I made following changes:

In service.js I made changes as follows:

var iam = process.env.IAM;
module.exports = {
        helloFriends: function (message) {
            console.log(message);
        }
    }
    
if (iam === 'service') {
    console.log('This part should not get invoked, When called by external modules other than service');
}

So based on caller configuration I decide whether to execute a particular code section or not.

The plugin used for .env https://www.npmjs.com/package/dotenv

Update:2

Based on learnings I had after this question better approach would be to have functions to serve each purpose/functionality & export them individually.

Prashant
  • 4,474
  • 8
  • 34
  • 82