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.

importing components using index.jsx

See original GitHub issue

The issue is so simple I spare a codepen.

I can import a component pointing to the directory only when there is a testComponent/index.js like this: import TestComponent from 'components/testComponent';

However this fails when the file has a jsx suffix instead of js: testComponent/index.jsx. Error message: no such file or directory, open '.\src\components\test\index.js' The jsx file is ignored, looking for a js file.

Is this intended?

Furthermore, I can import a file not named index like testComponent/test.js like this: import Test from 'components/testComponent/test';

I cannot however import the “index” like this: import Test from 'components/testComponent/index';

The error message is, surprise, no such file or directory, open '.\src\components\testComponent/index.jsx'. Now he’s looking for a jsx file and ignores the js file.

So, conclusio, if I want to use jsx accross the board in my project, I have to point to index files explicitly using the file name?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rschristiancommented, Jul 7, 2020

I just reproduced it again. Created fresh project from default template, started server, changed home/index.js to jsx

Build failed!

× ERROR ./routes/home/index.js
Module build failed (from ../node_modules/babel-loader/lib/index.js):
Error: ENOENT: no such file or directory, open '.\src\routes\home\index.js'

preact-cli 2.2.1 windows 10 Node 13.8.0

Firstly, don’t change the file extension of the entry point while the server is running. It doesn’t expect that to happen. However, if you restart the dev server, it shouldn’t have any issue resolving.

Edit: If I’m not mistaken, while the error will present itself in your terminal while running the server, using the app doesn’t seem to be impacted by this.

Secondly, are you actually using Preact-cli v2.2.1? The template should come with 3.0.0-rc.

1reaction
DamianRivascommented, Jul 5, 2020

Actually it works for me too. I’m not sure what I did wrong the first time, but I recently changed my filenames from the standard MyComponent/index.js to MyComponent/index.jsx and they imported just fine with import MyComponent from "components/MyComponent"

And you can see here that webpack is configured for it by default:

https://github.com/preactjs/preact-cli/blob/77e515daaa9df46d6bcf88eea321eac362b2f14b/packages/cli/lib/lib/webpack/webpack-base-config.js#L122

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do index.js files work in React component directories?
An index.js file allow you to import (or require() ) its parent folder as a module. This behavior is copied from Node.
Read more >
Import Components in React Like a Pro - The Startup - Medium
What if we could use only one file to import all the components? First, we will create an index.js (/ts,/.tsx,/.jsx) file. Then, we...
Read more >
Importing and Exporting Components - React Docs
Import it in the file where you'll use the component (using the corresponding technique for importing default or named exports). Here both Profile...
Read more >
Importing a Component | Create React App
Importing a Component. This project setup supports ES6 modules thanks to webpack. While you can still use require() and module.exports ...
Read more >
How to use index.js file(Properly) - DEV Community ‍ ‍
Tagged with javascript, typescript, react, webdev. ... now as the component is a default export , I can import the component by any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found