ThomasReggi
ThomasReggi

Reputation: 59485

Can I know, in deno, if my script is being run directly or being loaded by another script?

Parallel in node: Can I know, in node.js, if my script is being run directly or being loaded by another script?

I am looking for a way to tell if a deno script is being run direcly or if it's being imported by another module. Is this possible in deno? If so how?

Upvotes: 5

Views: 768

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40444

You have to use import.meta.main to know if a script is the entry point or not.

main.js

import child from './child.js';

console.log('Main', import.meta.main);

child.js

export default 'foo';
console.log('child', import.meta.main);

Now when you execute:

deno run main.js

You'll get:

child: false
Main: true

Upvotes: 11

Related Questions