Skip to content

Commit

Permalink
module: runtime deprecate invalid package.json main entries
Browse files Browse the repository at this point in the history
PR-URL: nodejs#37204
Fixes: nodejs#26588
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
aduh95 committed Feb 24, 2021
1 parent 80098e6 commit 76a073b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2350,12 +2350,15 @@ with no performance impact since Node.js 10.
### DEP0128: modules with an invalid `main` entry and an `index.js` file
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/37204
description: Runtime deprecation.
- version: v12.0.0
pr-url: https://github.com/nodejs/node/pull/26823
description: Documentation-only.
-->

Type: Documentation-only (supports [`--pending-deprecation`][])
Type: Runtime

Modules that have an invalid `main` entry (e.g., `./does-not-exist.js`) and
also have an `index.js` file in the top level directory will resolve the
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
err.requestPath = originalPath;
// TODO(BridgeAR): Add the requireStack as well.
throw err;
} else if (pendingDeprecation) {
} else {
const jsonPath = path.resolve(requestPath, 'package.json');
process.emitWarning(
`Invalid 'main' field in '${jsonPath}' of '${pkg}'. ` +
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-module-loading-deprecated.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Flags: --pending-deprecation

'use strict';

const common = require('../common');
Expand Down
12 changes: 10 additions & 2 deletions test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const path = require('path');

const backslash = /\\/g;

if (!process.env.NODE_PENDING_DEPRECATION)
process.on('warning', common.mustNotCall());
process.on('warning', common.mustCall());

console.error('load test-module-loading.js');

Expand Down Expand Up @@ -107,7 +106,16 @@ const d2 = require('../fixtures/b/d');
assert.strictEqual(require('../fixtures/packages/index').ok, 'ok');
assert.strictEqual(require('../fixtures/packages/main').ok, 'ok');
assert.strictEqual(require('../fixtures/packages/main-index').ok, 'ok');

common.expectWarning(
'DeprecationWarning',
"Invalid 'main' field in '" +
require.resolve('../fixtures/packages/missing-main/package.json') +
"' of 'doesnotexist.js'. Please either fix that or report it to the" +
' module author',
'DEP0128');
assert.strictEqual(require('../fixtures/packages/missing-main').ok, 'ok');

assert.throws(
() => require('../fixtures/packages/missing-main-no-index'),
{
Expand Down

0 comments on commit 76a073b

Please sign in to comment.