aboutsummaryrefslogtreecommitdiff
path: root/docs/api/import-meta.md
blob: 8e148fffaacc6fbeaa9851b3a44bc87c0082ce6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
The `import.meta` object is a way for a module to access information about itself. It's part of the JavaScript language, but its contents are not standardized. Each "host" (browser, runtime, etc) is free to implement any properties it wishes on the `import.meta` object.

Bun implements the following properties.

```ts#/path/to/project/file.ts
import.meta.dir;   // => "/path/to/project"
import.meta.file;  // => "file.ts"
import.meta.path;  // => "/path/to/project/file.ts"

import.meta.main;  // `true` if this file is directly executed by `bun run`
                   // `false` otherwise

import.meta.resolveSync("zod")
// resolve an import specifier relative to the directory
```

{% table %}

---

- `import.meta.dir`
- Absolute path to the directory containing the current fil, e.g. `/path/to/project`. Equivalent to `__dirname` in Node.js.

---

- `import.meta.file`
- The name of the current file, e.g. `index.tsx`. Equivalent to `__filename` in Node.js.

---

- `import.meta.path`
- Absolute path to the current file, e.g. `/path/to/project/index.tx`.

---

- `import.meta.main`
- `boolean` Indicates whether the current file is the entrypoint to the current `bun` process. Is the file being directly executed by `bun run` or is it being imported?

---

- `import.meta.resolve{Sync}`
- Resolve a module specifier (e.g. `"zod"` or `"./file.tsx`) to an absolute path. While file would be imported if the specifier were imported from this file?

  ```ts
  import.meta.resolveSync("zod");
  // => "/path/to/project/node_modules/zod/index.ts"

  import.meta.resolveSync("./file.tsx");
  // => "/path/to/project/file.tsx"
  ```

{% /table %}
d>-43/+63 2023-03-01Revert "Update clap (#2238)"Gravatar Jarred Sumner 16-290/+1840 2023-03-01Revert "Add `-D`, `--dev` flags for bun install (#2240)"Gravatar Jarred Sumner 1-9/+9 2023-03-01Use GitHub action ID instead of SHA for test workflowGravatar Ashcon Partovi 1-1/+1 2023-03-01avoids segfault after aborted onReject in Bun.serve streams (#2256)Gravatar Ciro Spaciari 1-7/+8 2023-03-01Run tests in CI for bun-linux-aarch64Gravatar Ashcon Partovi 2-1/+30 2023-03-01Revert spawnSync changeGravatar Jarred Sumner 1-1/+2 2023-03-01Update bindings.zigGravatar Jarred Sumner 1-1/+3 2023-03-01fix deinit behavior when connection is aborted using ResponseStream and abort...Gravatar Ciro Spaciari 3-34/+174 2023-03-01fix Bun.file.arrayBuffer() segmentation fault on empty file #2248 (#2249)Gravatar Ciro Spaciari 3-7/+23 2023-03-01Fix async in sqliteGravatar Colin McDonnell 1-2/+2 2023-02-28Forces a specific libdir for c-ares (#2241)Gravatar Justin Whear 1-1/+5 2023-02-28Make Bun.gc(true) more aggressiveGravatar Jarred Sumner 1-0/+3 2023-02-28Expose JSC::Options via `BUN_JSC_` prefixGravatar Jarred Sumner 6-8/+47 2023-02-28fixupGravatar Jarred Sumner 1-1/+1 2023-02-28Fix typecheckGravatar Colin McDonnell 2-1/+4 2023-02-28Fix incorrect Bun version in docs (#2236)Gravatar Derrick Farris 1-1/+1 2023-02-28just some comments fix (#2237)Gravatar Ciro Spaciari 1-4/+2 2023-02-28Add `-D`, `--dev` flags for bun install (#2240)Gravatar Justin Whear 1-9/+9 2023-02-28Document punningGravatar Colin McDonnell 1-1/+18