webpack: Module not found: Error: Can't resolve (with relative path)
Asked Answered
A

13

77

I have this structure of an app (node_modules dir excluded from this list):

├── actions.js
├── bundle.js
├── components
│   ├── App.js
│   ├── Footer.js
│   ├── Link.js
│   ├── Todo.js
│   └── TodoList.js
├── Containers
│   ├── AddTodo.js
│   ├── FilterLink.js
│   └── VisibleTodoList.js
├── index.html
├── index.js
├── main.js
├── package.json
├── package-lock.json
├── reducers.js
└── webpack.config.js

My webpack config looks like this:

module.exports = {
    entry: "./main.js",
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
        loaders: [
          {
            test: /\.js$/,
            loader: 'babel-loader',
            query: {
              presets: ['es2015', 'react']
            }
          }
        ]
    }
};

npm config:

{
  "name": "webpack-redux",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "nothing"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "babel-preset-es2015": "^6.24.1",
    "webpack": "^3.5.5"
  },
  "dependencies": {
    "react": "^15.6.1",
    "babel-preset-react": "^6.24.1",
    "react-dom": "^15.6.1",
    "react-redux": "^5.0.6",
    "redux": "^3.7.2"
  }
}

When I run webpack command, I get this error:

ERROR in ./components/App.js
Module not found: Error: Can't resolve '../containers/AddTodo' in '/home/oerp/js-programs/redux-test/components'
 @ ./components/App.js 11:15-47
 @ ./index.js
 @ ./main.js

ERROR in ./components/Footer.js
Module not found: Error: Can't resolve '../containers/FilterLink' in '/home/oerp/js-programs/redux-test/components'
 @ ./components/Footer.js 11:18-53
 @ ./components/App.js
 @ ./index.js
 @ ./main.js

ERROR in ./components/App.js
Module not found: Error: Can't resolve '../containers/VisibleTodoList' in '/home/oerp/js-programs/redux-test/components'
 @ ./components/App.js 15:23-63
 @ ./index.js
 @ ./main.js

My components/App.js content is this:

import Footer from './Footer'
import AddTodo from '../containers/AddTodo'
import VisibleTodoList from '../containers/VisibleTodoList'

const App = () => (
  <div>
    <AddTodo />
    <VisibleTodoList />
    <Footer />
  </div>
)

export default App

And for example containers/AddTodo.js:

import { connect } from 'react-redux'
import { addTodo } from '../actions'

let AddTodo = ({ dispatch }) => {
  let input

  return (
    <div>
      <form
        onSubmit={e => {
          e.preventDefault()
          if (!input.value.trim()) {
            return
          }
          dispatch(addTodo(input.value))
          input.value = ''
        }}
      >
        <input
          ref={node => {
            input = node
          }}
        />
        <button type="submit">
          Add Todo
        </button>
      </form>
    </div>
  )
}
AddTodo = connect()(AddTodo)

export default AddTodo

It seems it does not understand relative path with double dots, like ../something?

Do I need to configure webpack somehow for it to understand such paths?

Atlantis answered 30/8, 2017 at 19:25 Comment(2)
Had the same issue. I solved it here https://mcmap.net/q/188915/-module-not-found-error-can-39-t-resolve-39-util-39-in-webpackQuite
webpack --display-error-details should helpDissertation
E
75

Your file structure says that folder name is Container with a capital C. But you are trying to import it by container with a lowercase c. You will need to change the import or the folder name because the paths are case sensitive.

Eternalize answered 30/8, 2017 at 19:43 Comment(3)
This helped me too. windows had no problem with the file name beginning with capital but ubuntu had the problem.Misapprehend
I had a problem because I had an old webpack.config.js. We had switched to using a template webpack.config.babel.js and the old file was overriding the babel template when running webpack.Sailer
if you're somebody like me, developing on windows and CI/CD on Linux this happens. This solution helped me.Burch
S
75

I met this problem with typescript but forgot to add ts and tsx suffix to resolve entry.

module.exports = {
  ...
  resolve: {
    extensions: ['.js', '.jsx', '.ts', '.tsx'],
  },
};

This does the job for me

Schlock answered 15/1, 2021 at 5:6 Comment(3)
From docs it is better to include defaults like so: extensions: ['.ts', '...']Passerby
Which file exactly? Lots of files have the same format. :) Thanks a lot.Cultus
Okay, I found it. It is here: github.com/tleunen/babel-plugin-module-resolver/blob/HEAD/…Cultus
P
11

while importing libraries use the exact path to a file, including the directory relative to the current file, for example:

import Footer from './Footer/index.jsx'
import AddTodo from '../containers/AddTodo/index.jsx'
import VisibleTodoList from '../containers/VisibleTodoList/index.jsx'

Hope this may help

Preciado answered 7/6, 2019 at 4:38 Comment(3)
This isn't required, an import of import Footer from './Footer' is 100% validJoscelin
To be honest i do not recommend to put index file on each component directory. Better in my opinion is to use package.json file and indicate entry file in package.json, another option is to use Webapck directory plugin which i also do not recommend, because entire application will depend on webpackPreciado
Think it’s up to personal preference. At my company we have all Storybook stories, tests, CSS and supporting files in a directory for each component and it works great for us.Joscelin
F
11

You have to instruct the webpack to resolve the source code by using below code in webpack.config.js

module.exports={
   ...
   resolve:{
      extensions:['.js','.jsx'];
   }
}

Update : There is a new build tool "Vite" that aims to provide a faster and leaner development experience for modern web projects. Its is a plug and play tool, requires very less configuration.

Fibrinogen answered 8/2, 2021 at 4:45 Comment(2)
would you use vite for node projects without a front-end?Whisk
I renamed my jsx file to js and it just worked :). thanksFondness
C
7

Just ran into this... I have a common library shared among multiple transpiled products. I was using symlinks with brunch to handle sharing things between the projects. When moving to webpack, this stopped working.

What did get things working was using webpack configuration to turn off symlink resolving.

i.e. adding this in webpack.config.js:

module.exports = {
  //...
  resolve: {
    symlinks: false
  }
};

as documented here:

https://webpack.js.org/configuration/resolve/#resolvesymlinks

Conservator answered 26/7, 2020 at 16:4 Comment(0)
C
5

If you use multiple node_modules (yarn workspace etc), tell webpack where they are:

  externals: [nodeExternals({
    modulesDir: path.resolve(__dirname, '../node_modules'),
  }),  nodeExternals()],
Crunode answered 29/11, 2020 at 9:34 Comment(0)
L
1

I had this problem just after converting my project to TypeScript. The issue for me was that my project did not have a tsconfig.json file. My solution was to create a new TypeScript project, and copy the tsconfig.json file from the newly created project into my project.

Londoner answered 10/6, 2023 at 14:30 Comment(0)
P
0

I ran into a similar issue. I solved it by upgrading from the old [email protected] and [email protected] to [email protected] and [email protected].

Privet answered 10/5, 2023 at 23:31 Comment(0)
T
-1

Had the same issue with module not found. Turns out I had a component import Picture from './Picture/Picture'; at the very bottom of all the imports. When I moved it below import React, { Component } from 'react';, it worked.

Trichoid answered 2/6, 2018 at 4:2 Comment(1)
Sounds like a circular import issue.Wilow
W
-1

Just add it to your config. Source: https://www.jumoel.com/2017/zero-to-webpack.html

externals: [ nodeExternals() ]
Warms answered 15/10, 2019 at 12:53 Comment(2)
This is should be used only for backendCleopatracleopatre
Cannot find name 'nodeExternals'Embree
S
-1

Look the path for example this import is not correct import Navbar from '@/components/Navbar.vue' should look like this ** import Navbar from './components/Navbar.vue'**

Saltcellar answered 7/2, 2020 at 16:20 Comment(2)
I have a question.. How to get the vue base path? wish @ is vue base url, then I can use @/components/Navbar.vueMcdougald
@RobbyAlvianJayaMulia look into tsconfig.json "aliases" - you will also need to add these "aliases" to Webpack's resolversCytogenetics
U
-2

I had a different problem. some of my includes were set to 'app/src/xxx/yyy' instead of '../xxx/yyy'

Unhinge answered 16/9, 2020 at 17:30 Comment(0)
S
-3

changing templateUrl: '' to template: '' fixed it

Suburb answered 28/1, 2021 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.