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

Viewed 258
1 Answers

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
Related