question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable component import from parent directories

See original GitHub issue

It seems create-react-app@0.2.0 has problems importing modules from parent directories.

If I add a Hello component to the default generated app everything works as expected:

// src/App.js
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Hello from './Hello';

class App extends Component {
  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <Hello name="World"/>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

// src/Hello.js
import React from 'react';

const Hello = ({name}) => <p>Hello {name}!</p>

export default Hello;

But when I move Hello.js to the parent directory and point the import to it, it doesn’t work anymore:

// src/App.js
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Hello from '../Hello';

class App extends Component {
  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <Hello name="World"/>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

// Hello.js
import React from 'react';

const Hello = ({name}) => <p>Hello {name}!</p>

export default Hello;

Instead I get the following error:

Error in ./Hello.js
Module parse failed: /private/tmp/test/component-example/Hello.js Unexpected token (3:26)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (3:26)
 @ ./src/App.js 19:13-32

I would want to find a solution to use react-scripts in a subfolder (for devcards like component development) of a larger existing project. The react-script under the devcards subfolder would use the components from the main project (parent directory).

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Aug 4, 2016

Please see the README that should be generated in your project:

You may create subdirectories inside src. For faster rebuilds, only files inside src are processed by Webpack. You need to put any JS and CSS files inside src, or Webpack won’t see them.

We should add a warning for that…

0reactions
chrisbiancacommented, Sep 15, 2016

It would be nice to be able to customise this if possible. Not sure how easy that would be, but at the moment if you have a couple of apps that want to share some components it’s not possible without creating proper modules for them.

If somebody points me in the right direction, I’m happy to submit a pull request assuming that you decide it’s a worthwhile change to make.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React import from parent directory - Stack Overflow
Try this ./../Component.js . It works for me. More over if you need access a component from adjacent directory ./../AdjDir/Component.js.
Read more >
Python - Import from parent directory - GeeksforGeeks
In this article, we will learn how to Import a module from the parent directory. From Python 3.3, referencing or importing a module...
Read more >
How to import files from outside of root directory with React ...
So we're going to see how to directly reference any file outside of root directory.. Consider this example: common - components - Utils.ts,...
Read more >
How to Use File Choosers - Oracle Help Center
Given a parent component, creates and returns a new dialog that contains this file chooser, is dependent on the parent's frame, and is...
Read more >
importlib — The implementation of import — Python 3.11.1 ...
Two, the components to implement import are exposed in this package, ... To properly import a submodule you will need to import all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found