Skip to content

module: add createRequire method - #27405

Closed
MylesBorins wants to merge 2 commits into
nodejs:masterfrom
MylesBorins:createRequire
Closed

module: add createRequire method#27405
MylesBorins wants to merge 2 commits into
nodejs:masterfrom
MylesBorins:createRequire

Conversation

@MylesBorins

@MylesBorinsMylesBorins commented Apr 25, 2019

Copy link
Copy Markdown
Contributor

This is an abstraction on top of creatRequireFromPath that can accept
both paths, URL Strings, and URL Objects.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@MylesBorins
MylesBorinsforce-pushed the createRequire branch 2 times, most recently from a83b861 to 8c6a784CompareApril 25, 2019 06:20

@benjamingrbenjamingr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM

Comment threadtest/parallel/test-module-create-require.js Outdated
@mscdex

Copy link
Copy Markdown
Contributor

Is there a reason this can't be a part of createRequireFromPath() instead of creating a separate function?

Comment threadlib/internal/modules/cjs/loader.js Outdated
@targostargos added module Issues and PRs related to the module subsystem. semver-minor PRs that contain new features and should be released in the next minor version. labels Apr 25, 2019
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@MylesBorins

Copy link
Copy Markdown
ContributorAuthor

Updated code based on feedback from @devsnek. Have also added documentation to modules.md.

@mscdex I don't think this makes sense to add to createRequireFromPath as that API explicitly states it is meant for paths. I think it might make sense to deprecate createRequireFromPath and make it an internal API, as createRequire is far more useful IMHO.

@hybristhybrist left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@MylesBorins

Copy link
Copy Markdown
ContributorAuthor

CI is actually green the fail above is a lie. If there are no objections to this PR I plan to land it as is on Monday

@Fishrock123Fishrock123 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me... (but I am not convinced that it is an acceptable replacement of import.meta.require...)

Comment threadlib/internal/modules/cjs/loader.js Outdated
Comment threaddoc/api/modules.md Outdated
Comment threadtest/parallel/test-module-create-require.js Outdated
@MylesBorins

Copy link
Copy Markdown
ContributorAuthor

Changed a few things here:

  • createRequire now only supports absolute paths (either path or URL)
    • this is because the underlying logic in Module for relative paths is undefined / confusing e.g. caching cwd(). This limitation, which we could choose to remove later, guards against unexpected behavior (which was present in our documentation)
  • createRequireFromPath is now docs deprecated
    • please LMK if I need to do anything more, haven't deprecated before

PTAL

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment threadlib/internal/modules/cjs/loader.js Outdated
Comment threadlib/internal/modules/cjs/loader.js Outdated
@MylesBorins

Copy link
Copy Markdown
ContributorAuthor

One more stab at it

Differences:

  • now fails with non-url objects early
  • swallows all errors from fileURLToPath and has a single unified error
  • explicitly only support
    • file URL objects
    • file URL string
    • absolute path string

I believe this has scoped the use of this API enough that it guards against most footguns PTAL

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@BridgeARBridgeAR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we really deprecate the other API, it'll need an entry in doc/api/deprecations.md.

Comment threaddoc/api/modules.md Outdated
Comment threadlib/internal/modules/cjs/loader.js Outdated
Comment threadlib/internal/modules/cjs/loader.js Outdated
@benjamingr

Copy link
Copy Markdown
Member

changes still lgtm

@TrottTrott added the notable-change PRs with changes that should be highlighted in changelogs. label May 3, 2019
Comment threaddoc/api/deprecations.md Outdated
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@MylesBorins

Copy link
Copy Markdown
ContributorAuthor

latest CI was green!

landed in 411063c

MylesBorins added a commit that referenced this pull request May 3, 2019
This is an abstraction on top of creatRequireFromPath that can accept
both paths, URL Strings, and URL Objects.
PR-URL: #27405
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
targos pushed a commit that referenced this pull request May 4, 2019
This is an abstraction on top of creatRequireFromPath that can accept
both paths, URL Strings, and URL Objects.
PR-URL: #27405
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
@targostargos mentioned this pull request May 6, 2019
targos added a commit that referenced this pull request May 6, 2019
Notable changes:
* tls:
* Added an `enableTrace()` method to `TLSSocket` and an `enableTrace`
option to `tls.createServer()`. When enabled, TSL packet trace
information is written to `stderr`. This can be used to debug TLS
connection problems. #27497
* cli:
* Added a `--trace-tls` command-line flag that enables tracing of TLS
connections without the need to modify existing application code.
#27497
* Added a `--cpu-prof-interval` command-line flag. It can be used to
specify the sampling interval for the CPU profiles generated by
`--cpu-prof`. #27535
* module:
* Added the `createRequire()` method. It allows to create a require
function a file URL object, a file URL string or an absolute path
string. The existing `createRequireFromPath()` method is now
deprecated #27405.
* Throw on `require('./path.mjs')`. This is technically a breaking
change that should have landed with Node.js 12.0.0. It is necessary
to have this to keep the possibility for a future minor version to
load ES Modules with the require function.
#27417
* meta:
* Added Christian Clauss (https://github.com/cclauss) to
collaborators. #27554
PR-URL: #27578
targos added a commit that referenced this pull request May 7, 2019
Notable changes:
* deps:
* Updated llhttp to 1.1.3. This fixes a bug that made Node.js' HTTP
parser refuse any request URL that contained the "|" (vertical bar)
character. #27595
* tls:
* Added an `enableTrace()` method to `TLSSocket` and an `enableTrace`
option to `tls.createServer()`. When enabled, TSL packet trace
information is written to `stderr`. This can be used to debug TLS
connection problems. #27497#27376
* cli:
* Added a `--trace-tls` command-line flag that enables tracing of TLS
connections without the need to modify existing application code.
#27497
* Added a `--cpu-prof-interval` command-line flag. It can be used to
specify the sampling interval for the CPU profiles generated by
`--cpu-prof`. #27535
* module:
* Added the `createRequire()` method. It allows to create a require
function from a file URL object, a file URL string or an absolute
path string. The existing `createRequireFromPath()` method is now
deprecated #27405.
* Throw on `require('./path.mjs')`. This is technically a breaking
change that should have landed with Node.js 12.0.0. It is necessary
to have this to keep the possibility for a future minor version to
load ES Modules with the require function.
#27417
* repl:
* The REPL now supports multi-line statements using `BigInt` literals
as well as public and private class fields and methods.
#27400
* The REPL now supports tab autocompletion of file paths with `fs`
methods. #26648
* meta:
* Added Christian Clauss (https://github.com/cclauss) to
collaborators. #27554
PR-URL: #27578
targos added a commit that referenced this pull request May 7, 2019
Notable changes:
* deps:
* Updated llhttp to 1.1.3. This fixes a bug that made Node.js' HTTP
parser refuse any request URL that contained the "|" (vertical bar)
character. #27595
* tls:
* Added an `enableTrace()` method to `TLSSocket` and an `enableTrace`
option to `tls.createServer()`. When enabled, TSL packet trace
information is written to `stderr`. This can be used to debug TLS
connection problems. #27497#27376
* cli:
* Added a `--trace-tls` command-line flag that enables tracing of TLS
connections without the need to modify existing application code.
#27497
* Added a `--cpu-prof-interval` command-line flag. It can be used to
specify the sampling interval for the CPU profiles generated by
`--cpu-prof`. #27535
* module:
* Added the `createRequire()` method. It allows to create a require
function from a file URL object, a file URL string or an absolute
path string. The existing `createRequireFromPath()` method is now
deprecated #27405.
* Throw on `require('./path.mjs')`. This is technically a breaking
change that should have landed with Node.js 12.0.0. It is necessary
to have this to keep the possibility for a future minor version to
load ES Modules with the require function.
#27417
* repl:
* The REPL now supports multi-line statements using `BigInt` literals
as well as public and private class fields and methods.
#27400
* The REPL now supports tab autocompletion of file paths with `fs`
methods. #26648
* meta:
* Added Christian Clauss (https://github.com/cclauss) to
collaborators. #27554
PR-URL: #27578
abhishekumar-tyagi pushed a commit to abhishekumar-tyagi/node that referenced this pull request May 5, 2024
This is an abstraction on top of creatRequireFromPath that can accept
both paths, URL Strings, and URL Objects.
PR-URL: nodejs/node#27405
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

moduleIssues and PRs related to the module subsystem.notable-changePRs with changes that should be highlighted in changelogs.semver-minorPRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

16 participants

@MylesBorins@nodejs-github-bot@mscdex@BridgeAR@benjamingr@bnoordhuis@jasnell@hybrist@guybedford@Trott@SMotaal@addaleax@Fishrock123@devsnek@targos@BethGriggs