Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: add createRequire method #27405

Closed
wants to merge 2 commits into from

Conversation

@MylesBorins
Copy link
Member

commented Apr 25, 2019

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

This comment has been minimized.

@MylesBorins MylesBorins force-pushed the MylesBorins:createRequire branch 2 times, most recently from a83b861 to 8c6a784 Apr 25, 2019

@benjamingr
Copy link
Member

left a comment

Code LGTM

@mscdex

This comment has been minimized.

Copy link
Contributor

commented Apr 25, 2019

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

@nodejs-github-bot

This comment has been minimized.

@MylesBorins

This comment has been minimized.

Copy link
Member Author

commented Apr 26, 2019

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.

@jkrems

jkrems approved these changes Apr 26, 2019

Copy link
Contributor

left a comment

🎉

@nodejs-github-bot

This comment has been minimized.

@MylesBorins

This comment has been minimized.

Copy link
Member Author

commented Apr 26, 2019

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

@Fishrock123
Copy link
Member

left a comment

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

Show resolved Hide resolved doc/api/modules.md
Show resolved Hide resolved test/parallel/test-module-create-require.js Outdated

@MylesBorins MylesBorins force-pushed the MylesBorins:createRequire branch from 7412f8a to 21a6bf5 Apr 30, 2019

@MylesBorins

This comment has been minimized.

Copy link
Member Author

commented Apr 30, 2019

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

This comment has been minimized.

@sam-github sam-github referenced this pull request May 1, 2019

Closed

aix failing in CI #1796

@MylesBorins

This comment has been minimized.

Copy link
Member Author

commented May 1, 2019

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

@MylesBorins MylesBorins force-pushed the MylesBorins:createRequire branch from 29ca8e4 to bc5f646 May 1, 2019

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@BridgeAR
Copy link
Member

left a comment

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

Show resolved Hide resolved doc/api/modules.md Outdated
Show resolved Hide resolved lib/internal/modules/cjs/loader.js Outdated
Show resolved Hide resolved lib/internal/modules/cjs/loader.js
module: add createRequire method
This is an abstraction on top of creatRequireFromPath that can accept
both paths, URL Strings, and URL Objects.

@MylesBorins MylesBorins force-pushed the MylesBorins:createRequire branch from b99d8cf to e5bfad2 May 2, 2019

@nodejs-github-bot

This comment has been minimized.

@benjamingr

This comment has been minimized.

Copy link
Member

commented May 2, 2019

changes still lgtm

@Trott Trott added the notable-change label May 3, 2019

Show resolved Hide resolved doc/api/deprecations.md Outdated
@Trott

Trott approved these changes May 3, 2019

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@MylesBorins

This comment has been minimized.

Copy link
Member Author

commented May 3, 2019

latest CI was green!

landed in 411063c

@MylesBorins MylesBorins closed this May 3, 2019

MylesBorins added a commit that referenced this pull request May 3, 2019

module: add createRequire method
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 added a commit that referenced this pull request May 4, 2019

module: add createRequire method
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 added a commit that referenced this pull request May 6, 2019

2019-05-07, Version 12.2.0 (Current)
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: TODO

@targos targos referenced this pull request May 6, 2019

Merged

v12.2.0 proposal #27578

targos added a commit that referenced this pull request May 6, 2019

2019-05-07, Version 12.2.0 (Current)
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

2019-05-07, Version 12.2.0 (Current)
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

2019-05-07, Version 12.2.0 (Current)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.