patternjavascriptreactCritical
Can't resolve module (not found) in React.js
Viewed 0 times
foundreactmodulenotcanresolve
Problem
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
This is the
I've checked at least 10 times that the module is at this location
Yet, React still throws this error:
Failed to compile
npm test says the same thing.
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 componentimport 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 {
};
}
}
ReactDOM.render(, 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
./src/App.js
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.
Solution
The way we usually use
In your case,
Now, if from
As for
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.jsIn 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.Code Snippets
my-app/
node_modules/
package.json
src/
containers/card.js
components/header.js
App.js
index.jsContext
Stack Overflow Q#44439205, score: 208
Revisions (0)
No revisions yet.