javascript – Can't resolve module (not found) in React.js-ThrowExceptions

Exception or error:

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’ve checked at least 10 times for typos. Anyways, here’s the component code.

I want to render Header in root

import React, { Component } from 'react'
import Header from './src/components/header/header'
import logo from './logo.svg'
import './App.css'

class App extends Component {
  render() {
    return (

export default App;

This is the Header component

import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import navBar from './src/components/header/navBar'
import './src/css/header.css'

class Header extends Component {
    render() {
        return {
             <div id="particles-js"></div>

ReactDOM.render(<Header/>, document.getElementById('header'));

I’ve checked at least 10 times that the module is at this location ./src/components/header/header, and it is (folder “header” contains “header.js”).

Yet, React still throws this error:

Failed to compile

Module not found: Can't resolve './src/components/header/header' in '/home/wiseman/Desktop/React_Components/github-portfolio/src'

npm test says the same thing.

How to solve:

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.


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.


If you create an application with react-create-app, don’t forget set environment variable:


Or add to .env file to your root folder;


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.



I faced the same issue when I created a new react app, I tried all options in 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"


Deleted the package-lock.json file & then ran

npm install

Read further


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)
enter image description here


You need to be in project folder, if you are in src or public you have to come out of those folders. Suppose your react-project name is ‘hello-react’ then cd hello-react


I had a similar issue.


import HomeComponent from "components/HomeComponent";


import HomeComponent from "./components/HomeComponent";

NOTE: ./ was before components. You can read @Zac Kwan’s post above on how to use import


you should change import Header from ‘./src/components/header/header’ to

import Header from ‘../src/components/header/header’


You can try to execute ‘npm install’ in the app folder. This might also solve the problem. It worked for me.


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';


Check for the import statements.It should be ended with semicolon. If you miss any, you will get this error.

Also check whether following import statement added in you component.

import { threadId } from ‘worker_threads’;

If so remove that line. It works for me.


In my case I rename a component file, an VS Code add the below line of code for me:

import React, { Component } from "./node_modules/react";

So I fixed by removing the: ./node_modules/

import React, { Component } from "react";


Leave a Reply

Your email address will not be published. Required fields are marked *