diff --git a/config/paths.js b/config/paths.js index e6f2ff6cb3a..4570e4b4a5c 100644 --- a/config/paths.js +++ b/config/paths.js @@ -11,6 +11,7 @@ // and use those instead. This way we don't need to branch here. var path = require('path'); +var os = require('os'); // True after ejecting, false when used as a dependency var isEjected = ( @@ -31,6 +32,16 @@ function resolveApp(relativePath) { return path.resolve(relativePath); } +function resolveNodePath(paths) { + if (paths === '') { + return [] + } + var separator = os.platform() === 'win32' ? ';' : ':'; + return paths.split(separator).map(p => path.resolve(p)); +} + +var nodePath = resolveNodePath(process.env.NODE_PATH || ''); + if (isInCreateReactAppSource) { // create-react-app development: we're in ./config/ module.exports = { @@ -39,7 +50,8 @@ if (isInCreateReactAppSource) { appPackageJson: resolveOwn('../package.json'), appSrc: resolveOwn('../template/src'), appNodeModules: resolveOwn('../node_modules'), - ownNodeModules: resolveOwn('../node_modules') + ownNodeModules: resolveOwn('../node_modules'), + nodePath: nodePath }; } else if (!isEjected) { // before eject: we're in ./node_modules/react-scripts/config/ @@ -50,7 +62,8 @@ if (isInCreateReactAppSource) { appSrc: resolveApp('src'), appNodeModules: resolveApp('node_modules'), // this is empty with npm3 but node resolution searches higher anyway: - ownNodeModules: resolveOwn('../node_modules') + ownNodeModules: resolveOwn('../node_modules'), + nodePath: nodePath }; } else { // after eject: we're in ./config/ @@ -60,6 +73,7 @@ if (isInCreateReactAppSource) { appPackageJson: resolveApp('package.json'), appSrc: resolveApp('src'), appNodeModules: resolveApp('node_modules'), - ownNodeModules: resolveApp('node_modules') + ownNodeModules: resolveApp('node_modules'), + nodePath: nodePath }; } diff --git a/config/webpack.config.dev.js b/config/webpack.config.dev.js index 2b6da367f79..2a11970d6ad 100644 --- a/config/webpack.config.dev.js +++ b/config/webpack.config.dev.js @@ -65,6 +65,9 @@ module.exports = { publicPath: '/' }, resolve: { + // This allows you to set a root for where webpack should look for modules. + // This enables you to use absolute imports from the root. + root: paths.nodePath, // These are the reasonable defaults supported by the Node ecosystem. extensions: ['.js', '.json', ''], alias: { diff --git a/config/webpack.config.prod.js b/config/webpack.config.prod.js index 5781ae8636b..aa0263f8a59 100644 --- a/config/webpack.config.prod.js +++ b/config/webpack.config.prod.js @@ -60,6 +60,9 @@ module.exports = { publicPath: publicPath }, resolve: { + // This allows you to set a root for where webpack should look for modules. + // This enables you to use absolute imports from the root. + root: paths.nodePath, // These are the reasonable defaults supported by the Node ecosystem. extensions: ['.js', '.json', ''], alias: {