Skip to content

Commit

Permalink
fix(refactor): prefix built-in node module imports
Browse files Browse the repository at this point in the history
Since v5 relies on node >= 18, this is now possible (since v16, v14.18.0
[^1][^2]).

It's functionally irrelevant:
1. It's not required for CJS nor ESM (with a few exceptions [^3])
2. It has no performance promises

However, there are upsides to this approach:
1. It brings clear boundaries to what's a built-in and what's an
external dependency
2. It reduces the risk of importing unwanted deps where a built-in is
expected
3. It's slightly more interoperable with other JS runtimes that provide
node compatibility[^4], albeit only during development. Once imported
from npm, built-ins are assumed.

[^1]:https://nodejs.org/docs/latest-v22.x/api/modules.html#built-in-modules
[^2]:nodejs/node#37246
[^3]:https://nodejs.org/api/modules.html#built-in-modules-with-mandatory-node-prefix
[^4]:https://docs.deno.com/runtime/fundamentals/node/#using-node's-built-in-modules
  • Loading branch information
slagiewka authored and wesleytodd committed Jan 10, 2025
1 parent 6a40af8 commit 6ccc881
Show file tree
Hide file tree
Showing 52 changed files with 442 additions and 441 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ unreleased
* cleanup: remove unnecessary require for global Buffer
* perf: use loop for acceptParams
* Replace `methods` dependency with standard library
* refactor: prefix built-in node module imports

5.0.1 / 2024-10-08
==========
Expand Down
2 changes: 1 addition & 1 deletion examples/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

var express = require('../..');
var hash = require('pbkdf2-password')()
var path = require('path');
var path = require('node:path');
var session = require('express-session');

var app = module.exports = express();
Expand Down
2 changes: 1 addition & 1 deletion examples/downloads/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

var express = require('../../');
var path = require('path');
var path = require('node:path');

var app = module.exports = express();

Expand Down
2 changes: 1 addition & 1 deletion examples/ejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

var express = require('../../');
var path = require('path');
var path = require('node:path');

var app = module.exports = express();

Expand Down
2 changes: 1 addition & 1 deletion examples/error-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

var express = require('../../');
var path = require('path');
var path = require('node:path');
var app = module.exports = express();
var logger = require('morgan');
var silent = process.env.NODE_ENV === 'test'
Expand Down
4 changes: 2 additions & 2 deletions examples/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

var escapeHtml = require('escape-html');
var express = require('../..');
var fs = require('fs');
var fs = require('node:fs');
var marked = require('marked');
var path = require('path');
var path = require('node:path');

var app = module.exports = express();

Expand Down
2 changes: 1 addition & 1 deletion examples/mvc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

var express = require('../..');
var logger = require('morgan');
var path = require('path');
var path = require('node:path');
var session = require('express-session');
var methodOverride = require('method-override');

Expand Down
4 changes: 2 additions & 2 deletions examples/mvc/lib/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

var express = require('../../..');
var fs = require('fs');
var path = require('path');
var fs = require('node:fs');
var path = require('node:path');

module.exports = function(parent, options){
var dir = path.join(__dirname, '..', 'controllers');
Expand Down
2 changes: 1 addition & 1 deletion examples/route-separation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

var express = require('../..');
var path = require('path');
var path = require('node:path');
var app = express();
var logger = require('morgan');
var cookieParser = require('cookie-parser');
Expand Down
2 changes: 1 addition & 1 deletion examples/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

var express = require('../..');
var path = require('path');
var path = require('node:path');
var redis = require('redis');

var db = redis.createClient();
Expand Down
2 changes: 1 addition & 1 deletion examples/static-files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

var express = require('../..');
var logger = require('morgan');
var path = require('path');
var path = require('node:path');
var app = express();

// log requests
Expand Down
4 changes: 2 additions & 2 deletions examples/view-constructor/github-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Module dependencies.
*/

var https = require('https');
var path = require('path');
var https = require('node:https');
var path = require('node:path');
var extname = path.extname;

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/view-locals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

var express = require('../..');
var path = require('path');
var path = require('node:path');
var User = require('./user');
var app = express();

Expand Down
14 changes: 7 additions & 7 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
var finalhandler = require('finalhandler');
var debug = require('debug')('express:application');
var View = require('./view');
var http = require('http');
var http = require('node:http');
var methods = require('./utils').methods;
var compileETag = require('./utils').compileETag;
var compileQueryParser = require('./utils').compileQueryParser;
var compileTrust = require('./utils').compileTrust;
var resolve = require('path').resolve;
var resolve = require('node:path').resolve;
var once = require('once')
var Router = require('router');

Expand Down Expand Up @@ -468,8 +468,8 @@ app.disable = function disable(setting) {
* Delegate `.VERB(...)` calls to `router.VERB(...)`.
*/

methods.forEach(function(method){
app[method] = function(path){
methods.forEach(function (method) {
app[method] = function (path) {
if (method === 'get' && arguments.length === 1) {
// app.get(setting)
return this.set(path);
Expand Down Expand Up @@ -583,8 +583,8 @@ app.render = function render(name, options, callback) {
* and HTTPS server you may do so with the "http"
* and "https" modules as shown here:
*
* var http = require('http')
* , https = require('https')
* var http = require('node:http')
* , https = require('node:https')
* , express = require('express')
* , app = express();
*
Expand All @@ -595,7 +595,7 @@ app.render = function render(name, options, callback) {
* @public
*/

app.listen = function listen () {
app.listen = function listen() {
var server = http.createServer(this)
var args = Array.prototype.slice.call(arguments)
if (typeof args[args.length - 1] === 'function') {
Expand Down
2 changes: 1 addition & 1 deletion lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

var bodyParser = require('body-parser')
var EventEmitter = require('events').EventEmitter;
var EventEmitter = require('node:events').EventEmitter;
var mixin = require('merge-descriptors');
var proto = require('./application');
var Router = require('router');
Expand Down
4 changes: 2 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
*/

var accepts = require('accepts');
var isIP = require('net').isIP;
var isIP = require('node:net').isIP;
var typeis = require('type-is');
var http = require('http');
var http = require('node:http');
var fresh = require('fresh');
var parseRange = require('range-parser');
var parse = require('parseurl');
Expand Down
6 changes: 3 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ var contentDisposition = require('content-disposition');
var createError = require('http-errors')
var encodeUrl = require('encodeurl');
var escapeHtml = require('escape-html');
var http = require('http');
var http = require('node:http');
var onFinished = require('on-finished');
var mime = require('mime-types')
var path = require('path');
var pathIsAbsolute = require('path').isAbsolute;
var path = require('node:path');
var pathIsAbsolute = require('node:path').isAbsolute;
var statuses = require('statuses')
var sign = require('cookie-signature').sign;
var normalizeType = require('./utils').normalizeType;
Expand Down
4 changes: 2 additions & 2 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/

var debug = require('debug')('express:view');
var path = require('path');
var fs = require('fs');
var path = require('node:path');
var fs = require('node:fs');

/**
* Module variables.
Expand Down
2 changes: 1 addition & 1 deletion test/Route.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

var after = require('after');
var assert = require('assert')
var assert = require('node:assert')
var express = require('../')
, Route = express.Route
, methods = require('../lib/utils').methods
Expand Down
Loading

0 comments on commit 6ccc881

Please sign in to comment.