I can\'t believe that I\'m asking an obvious question, but I still get the error in console log.
Console says that it can\'t find the module in the directory, but I\
I had a similar issue.
Cause:
import HomeComponent from "components/HomeComponent";
Solution:
import HomeComponent from "./components/HomeComponent";
NOTE: ./
was before components. You can read @Zac Kwan's post above on how to use import
Adding NODE_PATH
as environment variable in .env
is deprecated and is replaced by adding "baseUrl": "./src"
, to compilerOptions
in jsconfig.json
or tsconfig.json
.
Reference
I think its the double use of header. I just tried something similar myself and also caused issues. I capitalized my component file to match the others and it worked.
import Header from './src/components/header/header';
Should be
import Header from './src/components/header/Header';
I faced the same issue when I created a new react app, I tried all options in https://github.com/facebook/create-react-app/issues/2534 but it didn't help. I had to change the port for the new app and then it worked. By default, apps use the port 3000.I changed the port to 8001 in package.json as follows:
"scripts": {
"start": "PORT=8001 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
The way we usually use import
is based on relative path.
.
and ..
are similar to how we use to navigate in terminal
like cd ..
to go out of directory and mv ~/file .
to move a file
to current directory.
my-app/
node_modules/
package.json
src/
containers/card.js
components/header.js
App.js
index.js
In your case, App.js
is in src/
directory while header.js
is in src/components
. To import
you would do import Header from './components/header'
. This roughly translate to in my current directory, find the components folder that contain a header file.
Now, if from header.js
, you need to import
something from card
, you would do this. import Card from '../containers/card'
. This translate to, move out of my current directory, look for a folder name containers that have a card file.
As for import React, { Component } from 'react'
, this does not start with a ./
or ../
or /
therefore node will start looking for the module in the node_modules
in a specific order till react
is found. For a more detail understanding, it can be read here.
I was facing the same problem and I resolved it.
See if your index.js
file is in src
folder, then what ever file you are importing, the folder containing that must also be inside the src folder.
That means if your components folder is outside the src
folder, just drag it inside the src
folder in your editor because the files outside of src
folder are not imported.
Then you shall be able to import using ./components/header/header
(in this case)