-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
65378cb
commit a7e393c
Showing
10 changed files
with
2,073 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Prefer JavaScript modules (ESM) over CommonJS | ||
|
||
Prefer using the [JavaScript module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) format over the legacy CommonJS module format. | ||
|
||
1. Forbids `'use strict'` directive. | ||
|
||
JavaScript modules use “Strict Mode” by default. | ||
|
||
1. Forbids “Global Return”. | ||
|
||
This is a CommonJS-only feature. | ||
|
||
1. Forbids the global variables `__dirname` and `__filename`. | ||
|
||
They are [not available in JavaScript modules](https://nodejs.org/api/esm.html#esm_no_filename_or_dirname). | ||
|
||
Replacements: | ||
|
||
```js | ||
import {fileURLToPath} from 'url'; | ||
import path from 'path'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
``` | ||
|
||
However, in most cases, this is better: | ||
|
||
```js | ||
import {fileURLToPath} from 'url'; | ||
|
||
const foo = fileURLToPath(new URL('foo.js', import.meta.url)); | ||
``` | ||
|
||
And many Node.js APIs accept `URL` directly, so you can just do this: | ||
|
||
```js | ||
const foo = new URL('foo.js', import.meta.url); | ||
``` | ||
|
||
1. Forbids `require(…)`. | ||
|
||
`require(…)` can be replaced by `import …` or `import(…)`. | ||
|
||
1. Forbids `exports` and `module.exports`. | ||
|
||
`export …` should be used in JavaScript modules. | ||
|
||
## Fail | ||
|
||
```js | ||
'use strict'; | ||
|
||
// … | ||
``` | ||
|
||
```js | ||
if (foo) { | ||
return; | ||
} | ||
|
||
// … | ||
``` | ||
|
||
```js | ||
const file = path.join(__dirname, 'foo.js'); | ||
``` | ||
|
||
```js | ||
const content = fs.readFileSync(__filename, 'utf8'); | ||
``` | ||
|
||
```js | ||
const {fromPairs} = require('lodash'); | ||
``` | ||
|
||
```js | ||
module.exports = foo; | ||
``` | ||
|
||
```js | ||
exports.foo = foo; | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
function run() { | ||
if (foo) { | ||
return; | ||
} | ||
|
||
// … | ||
} | ||
|
||
run(); | ||
``` | ||
|
||
```js | ||
const file = fileURLToPath(new URL('foo.js', import.meta.url)); | ||
``` | ||
```js | ||
import {fromPairs} from 'lodash-es'; | ||
``` | ||
```js | ||
export default foo; | ||
``` | ||
```js | ||
export {foo}; | ||
``` | ||
## Resources | ||
- [Get Ready For ESM](https://blog.sindresorhus.com/get-ready-for-esm-aa53530b3f77) by @sindresorhus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.