Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add ESM and CommonJS examples in fs documentation #52207

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
doc: add multiple missing mjs and cjs examples in fs.md
Signed-off-by: Tierney Cyren <hello@bnb.im>
  • Loading branch information
bnb committed Mar 25, 2024
commit 82747aaa98c2364b66eecd184fd293e0f52b860f
117 changes: 117 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,30 @@ for more details.
The `path` may be specified as a {FileHandle} that has been opened
for appending (using `fsPromises.open()`).

```mjs
import { appendFile } from 'node:fs/promises';

try {
await appendFile('message.txt', 'data to append');
} catch {
console.error('The file could not be appended!');
}
```

```cjs
const { appendFile } = require('node:fs/promises');

async function appendMessagesFile () {
try {
await appendFile('message.txt', 'data to append');
} catch {
console.error('The file could not be appended!');
}
}

appendMessagesFile()
```

### `fsPromises.chmod(path, mode)`

<!-- YAML
Expand All @@ -964,6 +988,30 @@ added: v10.0.0

Changes the permissions of a file.

```mjs
import { chmod } from 'node:fs/promises';

try {
await chmod('my_file.txt', 0o775).then(console.log('The permissions for file "my_file.txt" have been changed!'))
} catch {
console.log('The permissions for file "my_file.txt" failed to change!');
}
```

```cjs
const { chmod } = require('node:fs/promises');

async function changeFilePermissions () {
try {
await chmod('my_file.txt', 0o775).then(console.log('The permissions for file "my_file.txt" have been changed!'))
} catch {
console.log('The permissions for file "my_file.txt" failed to change!');
}
}

changeFilePermissions()
```

### `fsPromises.chown(path, uid, gid)`

<!-- YAML
Expand All @@ -977,6 +1025,38 @@ added: v10.0.0

Changes the ownership of a file.

```mjs
import { chown } from 'node:fs/promises'
import { getuid, getgid } from 'node:process'

const uid = getuid()
const gid = getgid()

try {
await chown("my_file.txt", uid, gid).then(console.log(`Successfully ran chown on my_file.txt to UID ${uid} and GID ${gid}!`))
} catch {
console.log("Failed to chown my_file.txt!")
}
```

```cjs
const { chown } = require('node:fs/promises')
const { getuid, getgid } = require('node:process')

async function chownMyFile () {
const uid = getuid()
const gid = getgid()

try {
await chown("my_file.txt", uid, gid).then(console.log(`Successfully ran chown on my_file.txt to UID ${uid} and GID ${gid}!`))
} catch {
console.log("Failed to chown my_file.txt!")
}
}

chownMyFile()
```

### `fsPromises.copyFile(src, dest[, mode])`

<!-- YAML
Expand Down Expand Up @@ -2254,6 +2334,15 @@ chmod('my_file.txt', 0o775, (err) => {
});
```

```cjs
const { chmod } = require('node:fs');

chmod('my_file.txt', 0o775, (err) => {
if (err) throw err;
console.log('The permissions for file "my_file.txt" have been changed!');
});
```

#### File modes

The `mode` argument used in both the `fs.chmod()` and `fs.chmodSync()`
Expand Down Expand Up @@ -2339,6 +2428,34 @@ possible exception are given to the completion callback.

See the POSIX chown(2) documentation for more detail.

```mjs
import { chown } from 'node:fs'
import { getuid, getgid } from 'node:process'

const uid = getuid()
const gid = getgid()

chown('my_file.txt', uid, gid, (err) => {
if (err) throw "Coudln't chown my_file.txt"

console.log(`Successfully ran chown on my_file.txt to UID ${uid} and GID ${gid}!`)
})
```

```cjs
const { chown } = require('node:fs')
const { getuid, getgid } = require('node:process')

const uid = getuid()
const gid = getgid()

chown('my_file.txt', uid, gid, (err) => {
if (err) throw "Coudln't chown my_file.txt"

console.log(`Successfully ran chown on my_file.txt to UID ${uid} and GID ${gid}!`)
})
```

### `fs.close(fd[, callback])`

<!-- YAML
Expand Down