Repository navigation
Improve module.createRequire documentation #28730
Description
Activity
It makes no sense to use createRequire with the module URL. And why rewrite "require" ?
As stated in another documentation page,
requireis not available on the global scope of a ES modules. This example is not to rewriterequire, but to create it. Using the module URL allows ES modules to userequirethe same way CJS files can.Also I think there is an error in the example code in the issue description:
> node test.js ./test.js:1 import { createRequire } from "module"; ^ SyntaxError: Unexpected token { at Module._compile (internal/modules/cjs/loader.js:720:23) at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10) at Module.load (internal/modules/cjs/loader.js:643:32) at Function.Module._load (internal/modules/cjs/loader.js:556:12) at Function.Module.runMain (internal/modules/cjs/loader.js:839:10) at internal/main/run_main_module.js:17:11importsyntax is not supported outside of ES2015 modules, so this code doesn't work in CJS mode.> node --experimental-modules test.mjs (node:15432) ExperimentalWarning: The ESM module loader is experimental. file:///.../test.mjs:2 const requireUtil = createRequire(require.resolve("../src/utils/")); ^ ReferenceError: require is not defined at file:///.../test.mjs:2:35 at ModuleJob.run (internal/modules/esm/module_job.js:111:37)requireis not defined, so this code doesn't work in ESM mode.IMHO that would probably be clearer to add an example using CJS, even though the function is more intended for ESM.
Reacted by Bo Lingen- addeddocIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.moduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
on Dec 26, 2020 The
import.meta.urlmeans a context for the newrequirefrom thecreateRequire?From fix cli imports PR
const createRequireConstructor = isAbsolute(from) ? from : resolve(from); const relativeRequire = createRequire(createRequireConstructor); // hereimport.meta.urlrefers to the URL of the current module. When you pass it tocreateRequire, it is indeed used for context to create therequirefunction.I think it's time to close this 2-year-old issue, anyone would be very welcome to open a PR to update
createRequiredocumentation if they think it can be improved, and feel free to re-open / to continue the discussion here if you want.
Is your feature request related to a problem? Please describe.
The documentation was unclear for createRequire, I see #27758 & #27762 made a change and adds
import.meta.url. I think this change adds difficulty (and needs more information) to understand this example.Also, the filename argument (path) can be a file, or URL. I don't understand why
../src/utils/was changed toimport.meta.url. It makes no sense to use createRequire with the module URL. And why rewrite "require" ?Describe the solution you'd like
Rewrite the example :
Describe alternatives you've considered
Another solution was: Add explain before the example and a link to
https://nodejs.org/api/esm.html#esm_import_metaSecond part :
Is your feature request related to a problem? Please describe.
The documentation was unclear for createRequire. I think the argument name path is more suited to filename in
module.createRequire(filename)According to : "Must be a file URL object, file URL string, or absolute path string."Describe the solution you'd like
Change filename to path.