From 8fca311857f0aefbba3782cef6a9812aa784d33f Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 19 Oct 2016 14:18:33 +0200 Subject: [PATCH 1/3] test: add more module loader test coverage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Verify that a package.json without a .main property loads index.js. PR-URL: https://github.com/nodejs/node/pull/9196 Reviewed-By: Brian White Reviewed-By: Colin Ihrig Reviewed-By: Evan Lucas Reviewed-By: Michaël Zasso Reviewed-By: James M Snell --- test/fixtures/packages/index/index.js | 1 + test/fixtures/packages/index/package.json | 1 + test/sequential/test-module-loading.js | 3 +++ 3 files changed, 5 insertions(+) create mode 100644 test/fixtures/packages/index/index.js create mode 100644 test/fixtures/packages/index/package.json diff --git a/test/fixtures/packages/index/index.js b/test/fixtures/packages/index/index.js new file mode 100644 index 00000000000000..014fa39dc365d1 --- /dev/null +++ b/test/fixtures/packages/index/index.js @@ -0,0 +1 @@ +exports.ok = 'ok'; diff --git a/test/fixtures/packages/index/package.json b/test/fixtures/packages/index/package.json new file mode 100644 index 00000000000000..0967ef424bce67 --- /dev/null +++ b/test/fixtures/packages/index/package.json @@ -0,0 +1 @@ +{} diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index 2f07bf55cf9766..d272b1921ba99e 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -69,6 +69,8 @@ assert.equal(threeFolder, threeIndex); assert.notEqual(threeFolder, three); console.error('test package.json require() loading'); +assert.equal(require('../fixtures/packages/index').ok, 'ok', + 'Failed loading package'); assert.equal(require('../fixtures/packages/main').ok, 'ok', 'Failed loading package'); assert.equal(require('../fixtures/packages/main-index').ok, 'ok', @@ -208,6 +210,7 @@ assert.deepStrictEqual(children, { }, 'fixtures/nested-index/three.js': {}, 'fixtures/nested-index/three/index.js': {}, + 'fixtures/packages/index/index.js': {}, 'fixtures/packages/main/package-main-module.js': {}, 'fixtures/packages/main-index/package-main-module/index.js': {}, 'fixtures/cycles/root.js': { From df29d9f5004a6d88603a9435782f4d7fcbcb3153 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 19 Oct 2016 15:38:30 +0200 Subject: [PATCH 2/3] doc: add performance warning to require.extensions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Discourage using require.extensions because it slows down the module loader. The number of file system operations that the module system has to perform in order to resolve a `require(...)` statement to a filename is proportional to the number of registered extensions. PR-URL: https://github.com/nodejs/node/pull/9196 Reviewed-By: Brian White Reviewed-By: Colin Ihrig Reviewed-By: Evan Lucas Reviewed-By: Michaël Zasso Reviewed-By: James M Snell --- doc/api/globals.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/api/globals.md b/doc/api/globals.md index 32acc983ffdd2e..885e2e09fc8879 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -216,10 +216,17 @@ However, in practice, there are much better ways to do this, such as loading modules via some other Node.js program, or compiling them to JavaScript ahead of time. -Since the Module system is locked, this feature will probably never go +Since the module system is locked, this feature will probably never go away. However, it may have subtle bugs and complexities that are best left untouched. +Note that the number of file system operations that the module system +has to perform in order to resolve a `require(...)` statement to a +filename scales linearly with the number of registered extensions. + +In other words, adding extensions slows down the module loader and +should be discouraged. + ### require.resolve()