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

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

我有这样的应用程序结构(node_modules 目录从该列表中排除):

├── 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

我的 webpack 配置如下所示:

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

npm 配置:

{
  "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"
  }
}

当我使用 运行 webpack 命令时,我得到这个错误:

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

我的components/App.js内容是这样的:

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

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

export default App

例如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

好像不理解带双点的相对路径,比如../something?

我是否需要以某种方式配置 webpack 以使其理解此类路径?

您的文件结构表明文件夹名称是 Container,大写 C。但是您正试图通过 container 和小写 c 导入它。您将需要更改导入或文件夹名称,因为路径区分大小写。

遇到与未找到模块相同的问题。原来我有一个组件 import Picture from './Picture/Picture'; 在所有导入的最底部。当我将它移到 import React, { Component } from 'react'; 以下时,它起作用了。

导入库时使用文件的确切路径,包括相对于当前文件的目录,例如:

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

希望这对您有所帮助

只需将其添加到您的配置中即可。资料来源:https://www.jumoel.com/2017/zero-to-webpack.html

externals: [ nodeExternals() ]

查看路径例如这个导入不正确 从“@/components/Navbar.vue”导入导航栏 应该是这样的 ** 从 './components/Navbar.vue' 导入导航栏**

只是 运行 进入这个...我有一个在多个 t运行 spiled 产品之间共享的公共库。我在早午餐中使用符号链接来处理项目之间的共享。当移动到 webpack 时,它停止工作了。

让事情正常进行的是使用 webpack 配置来关闭符号链接解析。

即在 webpack.config.js 中添加:

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

如此处所述:

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

我有一个不同的问题。我的一些包含设置为 'app/src/xxx/yyy' 而不是 '../xxx/yyy'

如果你使用多个 node_modules(yarn 工作区等),告诉 webpack 它们在哪里:

  externals: [nodeExternals({
    modulesDir: path.resolve(__dirname, '../node_modules'),
  }),  nodeExternals()],

我在使用 typescript 时遇到了这个问题,但忘记在 resolve 条目中添加 tstsx 后缀。

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

这适合我

templateUrl: '' 更改为 template: '' 修复了它

您必须使用以下代码指示 webpack 解析源代码 webpack.config.js

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

更新:有一个新的构建工具"Vite",旨在为现代网络项目提供更快、更精简的开发体验。它是一个即插即用的工具,需要的配置非常少。