Skip to content

Commit fd50986

Browse files
authoredNov 28, 2022
BREAKING: ESM support (#974)
* Remove process.cwd() trick from test files * BREAKING: Switch from main to exports * Add fs-extra/esm ESM named import module, with just fs-extra methods Fixes #746
1 parent 1a3205d commit fd50986

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+258
-42
lines changed
 

‎README.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Installation
2727
Usage
2828
-----
2929

30+
### CommonJS
31+
3032
`fs-extra` is a drop in replacement for native `fs`. All methods in `fs` are attached to `fs-extra`. All `fs` methods return promises if the callback isn't passed.
3133

3234
You don't ever need to include the original `fs` module again:
@@ -55,6 +57,31 @@ const fs = require('fs')
5557
const fse = require('fs-extra')
5658
```
5759

60+
### ESM
61+
62+
There is also an `fs-extra/esm` import, that supports both default and named exports. However, note that `fs` methods are not included in `fs-extra/esm`; you still need to import `fs` and/or `fs/promises` seperately:
63+
64+
```js
65+
import { readFileSync } from 'fs'
66+
import { readFile } from 'fs/promises'
67+
import { outputFile, outputFileSync } from 'fs-extra/esm'
68+
```
69+
70+
Default exports are supported:
71+
72+
```js
73+
import fs from 'fs'
74+
import fse from 'fs-extra/esm'
75+
// fse.readFileSync is not a function; must use fs.readFileSync
76+
```
77+
78+
but you probably want to just use regular `fs-extra` instead of `fs-extra/esm` for default exports:
79+
80+
```js
81+
import fs from 'fs-extra'
82+
// both fs and fs-extra methods are defined
83+
```
84+
5885
Sync vs Async vs Async/Await
5986
-------------
6087
Most methods are async by default. All async methods will return a promise if the callback isn't passed.
@@ -197,7 +224,8 @@ fs-extra contains hundreds of tests.
197224

198225
- `npm run lint`: runs the linter ([standard](http://standardjs.com/))
199226
- `npm run unit`: runs the unit tests
200-
- `npm test`: runs both the linter and the tests
227+
- `npm run unit-esm`: runs tests for `fs-extra/esm` exports
228+
- `npm test`: runs the linter and all tests
201229

202230

203231
### Windows

‎lib/copy/__tests__/ncp/broken-symlink.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require('fs')
44
const os = require('os')
5-
const fse = require(process.cwd())
5+
const fse = require('../../..')
66
const ncp = require('../../copy')
77
const path = require('path')
88
const assert = require('assert')

0 commit comments

Comments
 (0)
Please sign in to comment.