Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support src directory as webpack resolve.modules #3596

Closed
haraldrudell opened this issue Dec 14, 2017 · 3 comments
Closed

Support src directory as webpack resolve.modules #3596

haraldrudell opened this issue Dec 14, 2017 · 3 comments

Comments

@haraldrudell
Copy link

Much code that is pasted into Create React App assumes that the src directory is resolved so that filename and module references becomes the same.

people want to write
import 'components/Accordion'
instead of
import './components/Accordion'
because then at some point, components could be a module.
Drawback is that the thing won’t run no more unless using webpack resolver.

The suggestion is to modify webpack.config.js and such to:

  resolve: {
    modules: [
      path.resolve('src'),
      path.resolve('module_override'),
      'node_modules', .

(using the paths object, obviously)

from:

  resolve: {
    // This allows you to set a fallback for where Webpack should look for modules.
    // We placed these paths second because we want `node_modules` to "win"
    // if there are any conflicts. This matches Node resolution mechanism.
    // https://github.com/facebookincubator/create-react-app/issues/253
    modules: ['node_modules', paths.appNodeModules].concat(
@gaearon
Copy link
Contributor

gaearon commented Jan 9, 2018

You can already do this by putting

NODE_PATH=src

in a file called .env.

For a more permanent solution you can watch #1333.

@gaearon gaearon closed this as completed Jan 9, 2018
@hdr-js
Copy link

hdr-js commented Jan 31, 2018

@gaearon Thank you for the solution you provided above, But working with create-react-app and react-scripts, these .env and webpack.config.dev.js is in the node_modules directory. So we cannot update these with the entry NODE_PATH = src you mentioned above. Do create-react-app has a solution like, developer may provide the list of directories he want to include in the search for modules. In package.json or something.

@sanderpost
Copy link

sanderpost commented Mar 12, 2018

@haraldrudell you can place the .env file in the root of you application (next to package.json). It will be resolved automagically by the scripts. Also you can put any other REACT_APP_{yourvariable} prepended variables in the .env file and they will be accessible in your src using process.env.REACT_APP_{yourvariable} in your code.

@lock lock bot locked and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants