Skip to content

Commit

Permalink
document data loaders' cwd and import.meta.url
Browse files Browse the repository at this point in the history
closes #853
  • Loading branch information
Fil committed Mar 6, 2024
1 parent d0fae89 commit 125798f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/loaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ For example, for the file `quakes.csv`, the following data loaders are considere

If you use `.py`, `.R`, `.rs`, or `.go`, the corresponding interpreter (`python3`, `Rscript`, `rust-script`, or `go run`, respectively) must be installed and available on your `$PATH`. Any additional modules, packages, libraries, _etc._, must also be installed before you can use them.

Data loaders run from the same working directory as the `observable build` (or `preview`) command; in Node, you can access the current working directory by calling `process.cwd()`, and the script’s own location with [import.meta.url](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta). To compute the path of a file relative to the script location, use [import.meta.resolve](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve). For example, a data loader could read a file sitting next to it in the docs/ directory with the following command:

```js run=false
import {readFile} from "node:fs/promises";
import {fileURLToPath} from "node:url";

const table = await readFile(fileURLToPath(import.meta.resolve("./table.txt")), "utf-8");
...
```
Whereas `.js`, `.ts`, `.py`, `.R`, `.rs`, `.go`, and `.sh` data loaders are run via interpreters, `.exe` data loaders are run directly and must have the executable bit set. This is typically done via [`chmod`](https://en.wikipedia.org/wiki/Chmod). For example:
```sh
Expand Down

0 comments on commit 125798f

Please sign in to comment.