Skip to content

Commit c34555a

Browse files
gaearonromaindso
authored andcommitted
Always resolve NODE_PATH (facebook#2261)
* Always resolve NODE_PATH * Update env.js
1 parent 3703db9 commit c34555a

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

packages/react-scripts/config/env.js

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict';
1212

1313
const fs = require('fs');
14+
const path = require('path');
1415
const paths = require('./paths');
1516

1617
// Make sure that including paths.js after env.js will read .env variables.
@@ -46,6 +47,22 @@ dotenvFiles.forEach(dotenvFile => {
4647
}
4748
});
4849

50+
// We support resolving modules according to `NODE_PATH`.
51+
// This lets you use absolute paths in imports inside large monorepos:
52+
// https://github.com/facebookincubator/create-react-app/issues/253.
53+
// It works similar to `NODE_PATH` in Node itself:
54+
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
55+
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
56+
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
57+
// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
58+
// We also resolve them to make sure all tools using them work consistently.
59+
const appDirectory = fs.realpathSync(process.cwd());
60+
process.env.NODE_PATH = (process.env.NODE_PATH || '')
61+
.split(path.delimiter)
62+
.filter(folder => folder && !path.isAbsolute(folder))
63+
.map(folder => path.resolve(appDirectory, folder))
64+
.join(path.delimiter);
65+
4966
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
5067
// injected into the application via DefinePlugin in Webpack configuration.
5168
const REACT_APP = /^REACT_APP_/i;

packages/react-scripts/config/paths.js

+4-27
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,7 @@ const url = require('url');
1717
// Make sure any symlinks in the project folder are resolved:
1818
// https://github.com/facebookincubator/create-react-app/issues/637
1919
const appDirectory = fs.realpathSync(process.cwd());
20-
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
21-
22-
// We support resolving modules according to `NODE_PATH`.
23-
// This lets you use absolute paths in imports inside large monorepos:
24-
// https://github.com/facebookincubator/create-react-app/issues/253.
25-
26-
// It works similar to `NODE_PATH` in Node itself:
27-
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
28-
29-
// We will export `nodePaths` as an array of absolute paths.
30-
// It will then be used by Webpack configs.
31-
// Jest doesn’t need this because it already handles `NODE_PATH` out of the box.
32-
33-
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
34-
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
35-
// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
36-
37-
const nodePaths = (process.env.NODE_PATH || '')
38-
.split(process.platform === 'win32' ? ';' : ':')
39-
.filter(Boolean)
40-
.filter(folder => !path.isAbsolute(folder))
41-
.map(resolveApp);
20+
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
4221

4322
const envPublicUrl = process.env.PUBLIC_URL;
4423

@@ -53,7 +32,8 @@ function ensureSlash(path, needsSlash) {
5332
}
5433
}
5534

56-
const getPublicUrl = (appPackageJson) => envPublicUrl || require(appPackageJson).homepage;
35+
const getPublicUrl = appPackageJson =>
36+
envPublicUrl || require(appPackageJson).homepage;
5737

5838
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
5939
// "public path" at which the app is served.
@@ -80,13 +60,12 @@ module.exports = {
8060
yarnLockFile: resolveApp('yarn.lock'),
8161
testsSetup: resolveApp('src/setupTests.js'),
8262
appNodeModules: resolveApp('node_modules'),
83-
nodePaths: nodePaths,
8463
publicUrl: getPublicUrl(resolveApp('package.json')),
8564
servedPath: getServedPath(resolveApp('package.json')),
8665
};
8766

8867
// @remove-on-eject-begin
89-
const resolveOwn = (relativePath) => path.resolve(__dirname, '..', relativePath);
68+
const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
9069

9170
// config before eject: we're in ./node_modules/react-scripts/config/
9271
module.exports = {
@@ -101,7 +80,6 @@ module.exports = {
10180
yarnLockFile: resolveApp('yarn.lock'),
10281
testsSetup: resolveApp('src/setupTests.js'),
10382
appNodeModules: resolveApp('node_modules'),
104-
nodePaths: nodePaths,
10583
publicUrl: getPublicUrl(resolveApp('package.json')),
10684
servedPath: getServedPath(resolveApp('package.json')),
10785
// These properties only exist before ejecting:
@@ -131,7 +109,6 @@ if (
131109
yarnLockFile: resolveOwn('template/yarn.lock'),
132110
testsSetup: resolveOwn('template/src/setupTests.js'),
133111
appNodeModules: resolveOwn('node_modules'),
134-
nodePaths: nodePaths,
135112
publicUrl: getPublicUrl(resolveOwn('package.json')),
136113
servedPath: getServedPath(resolveOwn('package.json')),
137114
// These properties only exist before ejecting:

packages/react-scripts/config/webpack.config.dev.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ module.exports = {
8383
},
8484
resolve: {
8585
// This allows you to set a fallback for where Webpack should look for modules.
86-
// We read `NODE_PATH` environment variable in `paths.js` and pass paths here.
8786
// We placed these paths second because we want `node_modules` to "win"
8887
// if there are any conflicts. This matches Node resolution mechanism.
8988
// https://github.com/facebookincubator/create-react-app/issues/253
90-
modules: ['node_modules', paths.appNodeModules].concat(paths.nodePaths),
89+
modules: ['node_modules', paths.appNodeModules].concat(
90+
// It is guaranteed to exist because we tweak it in `env.js`
91+
process.env.NODE_PATH.split(path.delimiter)
92+
),
9193
// These are the reasonable defaults supported by the Node ecosystem.
9294
// We also include JSX as a common component filename extension to support
9395
// some tools, although we do not recommend using it, see:

packages/react-scripts/config/webpack.config.prod.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ module.exports = {
8181
},
8282
resolve: {
8383
// This allows you to set a fallback for where Webpack should look for modules.
84-
// We read `NODE_PATH` environment variable in `paths.js` and pass paths here.
8584
// We placed these paths second because we want `node_modules` to "win"
8685
// if there are any conflicts. This matches Node resolution mechanism.
8786
// https://github.com/facebookincubator/create-react-app/issues/253
88-
modules: ['node_modules', paths.appNodeModules].concat(paths.nodePaths),
87+
modules: ['node_modules', paths.appNodeModules].concat(
88+
// It is guaranteed to exist because we tweak it in `env.js`
89+
process.env.NODE_PATH.split(path.delimiter)
90+
),
8991
// These are the reasonable defaults supported by the Node ecosystem.
9092
// We also include JSX as a common component filename extension to support
9193
// some tools, although we do not recommend using it, see:

0 commit comments

Comments
 (0)