Skip to content

Commit 286866a

Browse files
authored
fix(core): Correctly search for loaded modules in node 12 (#2612)
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
1 parent deed353 commit 286866a

File tree

4 files changed

+66
-35
lines changed

4 files changed

+66
-35
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import process = require('process');
2+
3+
// process.versions.node is like "12.3.1"
4+
const [strMajor, strMinor, strPatch, ] = process.versions.node.split('.');
5+
6+
/**
7+
* The major version of the node runtime.
8+
*/
9+
export const major = Number(strMajor);
10+
11+
/**
12+
* The minor version of the node runtime.
13+
*/
14+
export const minor = Number(strMinor);
15+
16+
/**
17+
* The revision of the node runtime.
18+
*/
19+
export const patch = Number(strPatch);

packages/@aws-cdk/cdk/lib/runtime-info.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cxapi = require('@aws-cdk/cx-api');
2+
import { major as nodeMajorVersion } from './node-version';
23

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

5758
try {
58-
const packagePath = require.resolve('package.json', { paths });
59+
const packagePath = require.resolve(
60+
// Resolution behavior changed in node 12.0.0 - https://github.com/nodejs/node/issues/27583
61+
nodeMajorVersion >= 12 ? './package.json' : 'package.json',
62+
{ paths }
63+
);
5964
return require(packagePath);
6065
} catch (e) {
6166
return undefined;

packages/@aws-cdk/cdk/package-lock.json

+40-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/cdk/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
},
6868
"license": "Apache-2.0",
6969
"devDependencies": {
70-
"@types/lodash": "^4.14.123",
70+
"@types/lodash": "^4.14.130",
7171
"cdk-build-tools": "^0.31.0",
7272
"cfn2ts": "^0.31.0",
7373
"fast-check": "^1.14.0",

0 commit comments

Comments
 (0)