🚛 WTF is meta.main?

Run code in context with meta.main

Meta Main Example

file_type_typescript main.ts
export function helloWorld() {
  console.log('Main?', import.meta.main)
  return "Hi Mom!"
}

if (import.meta.main) {
  console.log(helloWorld());
}

Import the function in a different file:

file_type_typescript lib.ts
import { helloWorld } from './main'
helloWorld()

If you run the main file, the value is logged in side meta.main

command line
deno run main.ts

But if you run the lib file, it will NOT run the code inside meta.main

command line
deno run lib.ts

Questions? Let's chat

Open Discord