From 58a59a8d6ba2de899dd6093e644ee3cf0d4b9a0a Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 18 May 2019 09:10:20 -0400 Subject: [PATCH 1/2] doc: improve createRequire() example Update the example to use import and import.meta.url instead of require() and require.resolve(). PR-URL: https://github.com/nodejs/node/pull/27762 Fixes: https://github.com/nodejs/node/issues/27758 Reviewed-By: James M Snell Reviewed-By: Anto Aravinth --- .eslintrc.js | 1 + doc/api/modules.md | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index ff117624f5bd56..55b61cea63be6c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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', diff --git a/doc/api/modules.md b/doc/api/modules.md index 9266d43f49abcd..2be27fe6ce12dc 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -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) From 1b381d630a3a595405c00892c58335369a368915 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 18 May 2019 09:17:53 -0400 Subject: [PATCH 2/2] doc: replace createRequireFromPath() references This commit replaces createRequireFromPath() references with createRequire() references. PR-URL: https://github.com/nodejs/node/pull/27762 Fixes: https://github.com/nodejs/node/issues/27758 Reviewed-By: James M Snell Reviewed-By: Anto Aravinth --- doc/api/esm.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 60f9f6787dbcf4..646e7d5e9966ec 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -277,8 +277,7 @@ if this behavior is desired. These CommonJS variables are not available in ES modules. -`require` can be imported into an ES module using -[`module.createRequireFromPath()`][]. +`require` can be imported into an ES module using [`module.createRequire()`][]. An equivalent for `__filename` and `__dirname` is [`import.meta.url`][]. @@ -313,7 +312,7 @@ For now, only modules using the `file:` protocol can be loaded. `require` always treats the files it references as CommonJS. This applies whether `require` is used the traditional way within a CommonJS environment, or -in an ES module environment using [`module.createRequireFromPath()`][]. +in an ES module environment using [`module.createRequire()`][]. To include an ES module into CommonJS, use [`import()`][]. @@ -321,7 +320,7 @@ To include an ES module into CommonJS, use [`import()`][]. An `import` statement can reference either ES module or CommonJS JavaScript. Other file types such as JSON and Native modules are not supported. For those, -use [`module.createRequireFromPath()`][]. +use [`module.createRequire()`][]. `import` statements are permitted only in ES modules. For similar functionality in CommonJS, see [`import()`][]. @@ -362,14 +361,15 @@ to include ES module files from CommonJS code. ## CommonJS, JSON, and Native Modules -CommonJS, JSON, and Native modules can be used with [`module.createRequireFromPath()`][]. +CommonJS, JSON, and Native modules can be used with +[`module.createRequire()`][]. ```js // cjs.js module.exports = 'cjs'; // esm.mjs -import { createRequireFromPath as createRequire } from 'module'; +import { createRequire } from 'module'; import { fileURLToPath as fromURL } from 'url'; const require = createRequire(fromURL(import.meta.url)); @@ -759,7 +759,7 @@ success! [`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import [`import()`]: #esm_import-expressions [`import.meta.url`]: #esm_import_meta -[`module.createRequireFromPath()`]: modules.html#modules_module_createrequirefrompath_filename +[`module.createRequire()`]: modules.html#modules_module_createrequire_filename [CommonJS]: modules.html [ECMAScript-modules implementation]: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md [Node.js EP for ES Modules]: https://github.com/nodejs/node-eps/blob/master/002-es-modules.md