Skip to content

Commit 82066ac

Browse files
Jimmy Millergaearon
Jimmy Miller
authored andcommitted
Added ability to specify multiple directories in node_path.
1 parent 6e94bd8 commit 82066ac

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

config/paths.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
var path = require('path');
1313

14+
// We support resolving modules according to NODE_PATH.
15+
// This lets you use absolute paths in imports inside large monorepos:
16+
// https://github.com/facebookincubator/create-react-app/issues/253.
17+
// It works just like NODE_PATH in Node:
18+
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
19+
// We will export `nodePaths` as an array of absolute paths.
20+
// It will then be used by Webpack (and potentially other tools).
21+
var nodePaths = (process.env.NODE_PATH || '')
22+
.split(process.platform === 'win32' ? ';' : ':')
23+
.filter(Boolean)
24+
.map(p => path.resolve(p));
25+
1426
function resolveApp(relativePath) {
1527
return path.resolve(relativePath);
1628
}
@@ -22,13 +34,15 @@ module.exports = {
2234
appPackageJson: resolveApp('package.json'),
2335
appSrc: resolveApp('src'),
2436
appNodeModules: resolveApp('node_modules'),
25-
ownNodeModules: resolveApp('node_modules')
37+
ownNodeModules: resolveApp('node_modules'),
38+
nodePaths: nodePaths
2639
};
2740

2841
// @remove-on-eject-begin
2942
function resolveOwn(relativePath) {
3043
return path.resolve(__dirname, relativePath);
3144
}
45+
3246
// config before eject: we're in ./node_modules/react-scripts/config/
3347
module.exports = {
3448
appBuild: resolveApp('build'),
@@ -37,7 +51,8 @@ module.exports = {
3751
appSrc: resolveApp('src'),
3852
appNodeModules: resolveApp('node_modules'),
3953
// this is empty with npm3 but node resolution searches higher anyway:
40-
ownNodeModules: resolveOwn('../node_modules')
54+
ownNodeModules: resolveOwn('../node_modules'),
55+
nodePaths: nodePaths
4156
};
4257
// @remove-on-eject-end
4358

@@ -48,6 +63,7 @@ module.exports = {
4863
appPackageJson: resolveOwn('../package.json'),
4964
appSrc: resolveOwn('../template/src'),
5065
appNodeModules: resolveOwn('../node_modules'),
51-
ownNodeModules: resolveOwn('../node_modules')
66+
ownNodeModules: resolveOwn('../node_modules'),
67+
nodePaths: nodePaths
5268
};
5369
// @remove-on-publish-end

config/webpack.config.dev.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ module.exports = {
6767
publicPath: '/'
6868
},
6969
resolve: {
70-
// This allows you to set a root for where webpack should look for modules.
71-
// This enables you to use absolute imports from the root.
72-
root: path.resolve(process.env.NODE_PATH || ''),
70+
// This allows you to set a root for where Webpack should look for modules.
71+
// It must be an absolute path or an array of absolute paths.
72+
// This lets you use absolute paths in imports inside large monorepos:
73+
// https://github.com/facebookincubator/create-react-app/issues/253.
74+
root: paths.nodePaths,
7375
// These are the reasonable defaults supported by the Node ecosystem.
7476
extensions: ['.js', '.json', ''],
7577
alias: {

config/webpack.config.prod.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ module.exports = {
6262
publicPath: publicPath
6363
},
6464
resolve: {
65-
// This allows you to set a root for where webpack should look for modules.
66-
// This enables you to use absolute imports from the root.
67-
root: path.resolve(process.env.NODE_PATH || ''),
65+
// This allows you to set a root for where Webpack should look for modules.
66+
// It must be an absolute path or an array of absolute paths.
67+
// This lets you use absolute paths in imports inside large monorepos:
68+
// https://github.com/facebookincubator/create-react-app/issues/253.
69+
root: paths.nodePaths,
6870
// These are the reasonable defaults supported by the Node ecosystem.
6971
extensions: ['.js', '.json', ''],
7072
alias: {

0 commit comments

Comments
 (0)