Skip to content

Commit fd50986

Browse files
authored
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')

lib/copy/__tests__/ncp/ncp-error-perm.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
const fs = require('fs')
66
const os = require('os')
7-
const fse = require(process.cwd())
7+
const fse = require('../../..')
88
const ncp = require('../../copy')
99
const path = require('path')
1010
const assert = require('assert')

lib/copy/__tests__/ncp/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')

lib/empty/__tests__/empty-dir-sync.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 path = require('path')
77
const assert = require('assert')
88

lib/empty/__tests__/empty-dir.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 path = require('path')
77
const assert = require('assert')
88

lib/ensure/__tests__/create.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 path = require('path')
77
const assert = require('assert')
88

lib/ensure/__tests__/ensure.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 path = require('path')
77
const assert = require('assert')
88

lib/ensure/__tests__/link.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const CWD = process.cwd()
44

55
const fs = require('graceful-fs')
66
const os = require('os')
7-
const fse = require(CWD)
7+
const fse = require('../..')
88
const path = require('path')
99
const assert = require('assert')
1010
const ensureLink = fse.ensureLink

lib/ensure/__tests__/symlink-paths.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const CWD = process.cwd()
44

55
const fs = require('graceful-fs')
66
const os = require('os')
7-
const fse = require(CWD)
7+
const fse = require('../..')
88
const path = require('path')
99
const assert = require('assert')
1010
const _symlinkPaths = require('../symlink-paths')

lib/ensure/__tests__/symlink-type.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const CWD = process.cwd()
44

55
const fs = require('graceful-fs')
66
const os = require('os')
7-
const fse = require(CWD)
7+
const fse = require('../..')
88
const path = require('path')
99
const assert = require('assert')
1010
const _symlinkType = require('../symlink-type')

lib/ensure/__tests__/symlink.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const CWD = process.cwd()
44

55
const fs = require('graceful-fs')
66
const os = require('os')
7-
const fse = require(CWD)
7+
const fse = require('../..')
88
const path = require('path')
99
const assert = require('assert')
1010
const _symlinkPaths = require('../symlink-paths')

lib/esm.mjs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import _copy from './copy/index.js'
2+
import _empty from './empty/index.js'
3+
import _ensure from './ensure/index.js'
4+
import _json from './json/index.js'
5+
import _mkdirs from './mkdirs/index.js'
6+
import _move from './move/index.js'
7+
import _outputFile from './output-file/index.js'
8+
import _pathExists from './path-exists/index.js'
9+
import _remove from './remove/index.js'
10+
11+
// NOTE: Only exports fs-extra's functions; fs functions must be imported from "node:fs" or "node:fs/promises"
12+
13+
export const copy = _copy.copy
14+
export const copySync = _copy.copySync
15+
export const emptyDirSync = _empty.emptyDirSync
16+
export const emptydirSync = _empty.emptydirSync
17+
export const emptyDir = _empty.emptyDir
18+
export const emptydir = _empty.emptydir
19+
export const createFile = _ensure.createFile
20+
export const createFileSync = _ensure.createFileSync
21+
export const ensureFile = _ensure.ensureFile
22+
export const ensureFileSync = _ensure.ensureFileSync
23+
export const createLink = _ensure.createLink
24+
export const createLinkSync = _ensure.createLinkSync
25+
export const ensureLink = _ensure.ensureLink
26+
export const ensureLinkSync = _ensure.ensureLinkSync
27+
export const createSymlink = _ensure.createSymlink
28+
export const createSymlinkSync = _ensure.createSymlinkSync
29+
export const ensureSymlink = _ensure.ensureSymlink
30+
export const ensureSymlinkSync = _ensure.ensureSymlinkSync
31+
export const readJson = _json.readJson
32+
export const readJSON = _json.readJSON
33+
export const readJsonSync = _json.readJsonSync
34+
export const readJSONSync = _json.readJSONSync
35+
export const writeJson = _json.writeJson
36+
export const writeJSON = _json.writeJSON
37+
export const writeJsonSync = _json.writeJsonSync
38+
export const writeJSONSync = _json.writeJSONSync
39+
export const outputJson = _json.outputJson
40+
export const outputJSON = _json.outputJSON
41+
export const outputJsonSync = _json.outputJsonSync
42+
export const outputJSONSync = _json.outputJSONSync
43+
export const mkdirs = _mkdirs.mkdirs
44+
export const mkdirsSync = _mkdirs.mkdirsSync
45+
export const mkdirp = _mkdirs.mkdirp
46+
export const mkdirpSync = _mkdirs.mkdirpSync
47+
export const ensureDir = _mkdirs.ensureDir
48+
export const ensureDirSync = _mkdirs.ensureDirSync
49+
export const move = _move.move
50+
export const moveSync = _move.moveSync
51+
export const outputFile = _outputFile.outputFile
52+
export const outputFileSync = _outputFile.outputFileSync
53+
export const pathExists = _pathExists.pathExists
54+
export const pathExistsSync = _pathExists.pathExistsSync
55+
export const remove = _remove.remove
56+
export const removeSync = _remove.removeSync
57+
58+
export default {
59+
..._copy,
60+
..._empty,
61+
..._ensure,
62+
..._json,
63+
..._mkdirs,
64+
..._move,
65+
..._outputFile,
66+
..._pathExists,
67+
..._remove
68+
}

lib/fs/__tests__/realpath.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('realpath.native does not exist', () => {
1818

1919
const realpathNativeBackup = fs.realpath.native
2020
const clearFseCache = () => {
21-
const fsePath = path.dirname(require.resolve('../../..'))
21+
const fsePath = path.dirname(require.resolve('../..'))
2222
for (const entry in require.cache) {
2323
if (entry.startsWith(fsePath)) {
2424
delete require.cache[entry]

lib/json/__tests__/jsonfile-integration.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 path = require('path')
77
const assert = require('assert')
88

lib/json/__tests__/output-json-sync.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 path = require('path')
77
const assert = require('assert')
88

lib/json/__tests__/output-json.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 path = require('path')
77
const assert = require('assert')
88

lib/json/__tests__/promise-support.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 path = require('path')
77
const assert = require('assert')
88

lib/json/__tests__/read.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 path = require('path')
77
const assert = require('assert')
88

lib/mkdirs/__tests__/clobber.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 path = require('path')
77
const assert = require('assert')
88

lib/mkdirs/__tests__/issue-209.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const assert = require('assert')
4-
const fse = require(process.cwd())
4+
const fse = require('../..')
55

66
/* global describe, it */
77

lib/mkdirs/__tests__/issue-93.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const os = require('os')
4-
const fse = require(process.cwd())
4+
const fse = require('../..')
55
const path = require('path')
66
const assert = require('assert')
77
const util = require('util')

lib/mkdirs/__tests__/mkdir.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 path = require('path')
77
const assert = require('assert')
88

lib/mkdirs/__tests__/mkdirp.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 path = require('path')
77
const assert = require('assert')
88

lib/mkdirs/__tests__/opts-undef.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 path = require('path')
77
const assert = require('assert')
88

lib/mkdirs/__tests__/perm_sync.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 path = require('path')
77
const assert = require('assert')
88

lib/mkdirs/__tests__/race.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 path = require('path')
77
const assert = require('assert')
88

lib/mkdirs/__tests__/rel.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const CWD = process.cwd()
44

55
const fs = require('fs')
66
const os = require('os')
7-
const fse = require(CWD)
7+
const fse = require('../..')
88
const path = require('path')
99
const assert = require('assert')
1010

lib/mkdirs/__tests__/sync.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 path = require('path')
77
const assert = require('assert')
88

lib/move/__tests__/move-sync.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// const fs = require('graceful-fs')
55
const fs = require('fs')
66
const os = require('os')
7-
const fse = require(process.cwd())
7+
const fse = require('../..')
88
const path = require('path')
99
const assert = require('assert')
1010

lib/output-file/__tests__/output.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 path = require('path')
77
const assert = require('assert')
88

lib/path-exists/__tests__/path-exists-sync.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
/* eslint-env mocha */
33

4-
const fs = require(process.cwd())
4+
const fs = require('../..')
55
const path = require('path')
66
const os = require('os')
77
const assert = require('assert')

lib/path-exists/__tests__/path-exists.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
/* eslint-env mocha */
33

4-
const fs = require(process.cwd())
4+
const fs = require('../..')
55
const path = require('path')
66
const os = require('os')
77
const assert = require('assert')

0 commit comments

Comments
 (0)