ashes999
ashes999

Reputation: 1324

React module not found - how to import modules in react

I recently started learning React, but right now I am having an issue import one of the components in App.js

My current structure is:

src
--components
----Nav
------Nav.jsx
--App.js

Inside my App.js I have:

import Nav from './src/components/Nav/Nav';

However, I get the following error:

./src/App.js
Module not found: Can't resolve './src/components/Nav/Nav' in 'PATH_TO_PROJECT/project/src'

My webpack.config.js is:

const path = require("path");

module.exports = {
    entry: "./src/app.js",
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js'
    },
    module:{
        rules:[{
            loader: 'babel-loader',
            test: /\.js$|jsx/,
            exclude: /node_modules/
        }],
        exports: {
            resolve: {
                extensions:['.js','.jsx']
            }
        }
    },
    devtool: 'cheap-module-eval-source-map',
    devServer: {
        contentBase: path.join(__dirname, 'public')
    }
}

And this is also my package.json:

{
  "name": "dashboard",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@testing-library/user-event": "^12.7.1",
    "bootstrap": "^4.6.0",
    "express": "^4.17.1",
    "react": "^17.0.1",
    "react-bootstrap": "^1.5.0",
    "react-dom": "^17.0.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.2",
    "web-vitals": "^1.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "webpack",
    "test": "webpack serve",
    "eject": "webpack --watch"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/core": "^7.12.3",
    "@babel/preset-env": "^7.12.17",
    "@babel/preset-react": "^7.12.13",
    "babel-loader": "^8.1.0",
    "webpack": "^4.44.2",
    "webpack-cli": "^4.5.0",
    "webpack-dev-server": "^3.11.0"
  }
}

I was wondering what am I doing wrong?

Upvotes: 2

Views: 1240

Answers (1)

NeERAJ TK
NeERAJ TK

Reputation: 2695

The error is occurring due to the wrong project path specification. The src is the source directory of a react application. When importing use the following

import Nav from './components/Nav/Nav';

Upvotes: 1

Related Questions