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.