Skip to content

Commit

Permalink
fix(core): Correctly search for loaded modules in node 12 (#2612)
Browse files Browse the repository at this point in the history
Node 12 has corrected how the require.resolve function applied un-documented behavior, so
that it is needed to make relative search paths explicit (./).

Related: nodejs/node#27583
  • Loading branch information
RomainMuller authored May 23, 2019
1 parent deed353 commit 286866a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 35 deletions.
19 changes: 19 additions & 0 deletions packages/@aws-cdk/cdk/lib/node-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import process = require('process');

// process.versions.node is like "12.3.1"
const [strMajor, strMinor, strPatch, ] = process.versions.node.split('.');

/**
* The major version of the node runtime.
*/
export const major = Number(strMajor);

/**
* The minor version of the node runtime.
*/
export const minor = Number(strMinor);

/**
* The revision of the node runtime.
*/
export const patch = Number(strPatch);
7 changes: 6 additions & 1 deletion packages/@aws-cdk/cdk/lib/runtime-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cxapi = require('@aws-cdk/cx-api');
import { major as nodeMajorVersion } from './node-version';

/**
* Returns a list of loaded modules and their versions.
Expand Down Expand Up @@ -55,7 +56,11 @@ function findNpmPackage(fileName: string): { name: string, version: string, priv
const paths = mod.paths.map(stripNodeModules);

try {
const packagePath = require.resolve('package.json', { paths });
const packagePath = require.resolve(
// Resolution behavior changed in node 12.0.0 - https://github.com/nodejs/node/issues/27583
nodeMajorVersion >= 12 ? './package.json' : 'package.json',
{ paths }
);
return require(packagePath);
} catch (e) {
return undefined;
Expand Down
73 changes: 40 additions & 33 deletions packages/@aws-cdk/cdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/@aws-cdk/cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@types/lodash": "^4.14.123",
"@types/lodash": "^4.14.130",
"cdk-build-tools": "^0.31.0",
"cfn2ts": "^0.31.0",
"fast-check": "^1.14.0",
Expand Down

0 comments on commit 286866a

Please sign in to comment.