# \`import.meta.isEntry\` to detect top-level root module(s)

**URL:** <https://es.discourse.group/t/import-meta-isentry-to-detect-top-level-root-module-s/1842>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [November 3, 2023, 10:30pm UTC](https://es.discourse.group/t/import-meta-isentry-to-detect-top-level-root-module-s/1842 "2023-11-03T22:30:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trusktr](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/trusktr/32/183_2.png) [@trusktr](https://es.discourse.group/u/trusktr)\
**Post date:** [November 3, 2023, 10:30pm UTC](https://es.discourse.group/t/import-meta-isentry-to-detect-top-level-root-module-s/1842/1 "2023-11-03T22:30:04Z")

</div>

We can do this in Node.js fairly easily, but it would be useful to have in browsers too.

```js
import {foo} from 'https://web.site/path/to/lib.js'

if (import.meta.isEntry) {
  // if this is top level, we might show a demo of `doSomethingWithFoo`
  console.log(doSomethingWithFoo(foo))
}

export function doSomethingWithFoo() {}

```

And likewise, we might need this:

```js
import('https://web.site/path/to/lib.js', {
  isEntry: true // false by default seems good for `import()`, otherwise it'll break existing import()s of libs that are not top-level entries (most of them probably)
})

```

# Real-world example

A file like this one,

[https://codepen.io/ScavengerFrontend/pen/xxGwjox.js](https://codepen.io/ScavengerFrontend/pen/xxGwjox.js)

which is from this pen:

[https://codepen.io/ScavengerFrontend/pen/xxGwjox](https://codepen.io/ScavengerFrontend/pen/xxGwjox)

could replace this,

```js
init();
render();

```

with

```js
if (import.meta.isEntry) {
  init();
  render();
}

```

and it could additional prepend `export` to `createCactus` like this,

```js
export function createCactus() {

```

The result would be that the pen works as a standalone demo, but any other pens (or any other ESM code anywhere on the internet) could import it and use the cactus shapes in their creations:

```js
import {createCactus} from 'https://codepen.io/ScavengerFrontend/pen/xxGwjox.js'

const cactus = createCactus()

// ...

```

---

<div class="post-metadata">

**Author:** ![trusktr](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/trusktr/32/183_2.png) [@trusktr](https://es.discourse.group/u/trusktr)\
**Post date:** [November 3, 2023, 10:39pm UTC](https://es.discourse.group/t/import-meta-isentry-to-detect-top-level-root-module-s/1842/2 "2023-11-03T22:39:41Z")

</div>

I've created a workaround to achieve something like this specifically for my codepen demos. For example, in this pen, I define a few custom elements for rendering flickering lights:

https://codepen.io/trusktr/embed/preview/YzGbeKG?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=YzGbeKG

Note at the bottom of the JS it has this, which is in a similar vein:

```js
// Run code only if this script is running in this pen. Otherwise other pens may import the above.
if (document.querySelector("#YzGbeKG")) {
	lights.rotation = (x, y, z) => [x, y + 0.2, z];
}

```

Then here's another pen,

https://codepen.io/trusktr/embed/preview/oNzRKJN?default-tabs=html%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=oNzRKJN

that imports the light elements,

```js
import "https://codepen.io/trusktr/pen/YzGbeKG.js?4";

```

and places them around a different object

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [November 3, 2023, 10:45pm UTC](https://es.discourse.group/t/import-meta-isentry-to-detect-top-level-root-module-s/1842/3 "2023-11-03T22:45:48Z")

</div>

Considering how it's generally become a bad practice to do this in node - eg, to have a file that doubles as an entry point and as an importable thing - I think the best thing to do here is continue to encourage having single-purpose files.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [November 8, 2023, 9:39am UTC](https://es.discourse.group/t/import-meta-isentry-to-detect-top-level-root-module-s/1842/4 "2023-11-08T09:39:16Z")

</div>

Some prior conversation on this topic here: [Add `import.meta` · Issue #46 · wintercg/proposal-common-minimum-api · GitHub](https://github.com/wintercg/proposal-common-minimum-api/issues/46)
