Skip to content

Commit

Permalink
doc: improve createRequire() example
Browse files Browse the repository at this point in the history
Update the example to use import and import.meta.url instead
of require() and require.resolve().

PR-URL: nodejs#27762
Fixes: nodejs#27758
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
  • Loading branch information
cjihrig committed May 20, 2019
1 parent 64182e9 commit 58a59a8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = {
{
files: [
'doc/api/esm.md',
'doc/api/modules.md',
'test/es-module/test-esm-type-flag.js',
'test/es-module/test-esm-type-flag-alias.js',
'*.mjs',
Expand Down
8 changes: 4 additions & 4 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -923,11 +923,11 @@ added: v12.2.0
* Returns: {require} Require function

```js
const { createRequire } = require('module');
const requireUtil = createRequire(require.resolve('../src/utils/'));
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

// Require `../src/utils/some-tool`
requireUtil('./some-tool');
// sibling-module.js is a CommonJS module.
const siblingModule = require('./sibling-module');
```
### module.createRequireFromPath(filename)
Expand Down

0 comments on commit 58a59a8

Please sign in to comment.