ERROR in Entry module not found: Error: Can't resolve './src' in XXX的一個解決方案


ERROR in Entry module not found: Error: Can't resolve './src' in XXX的一個解決方案

踩坑場景

今天在學習 Vue 的 webpack 時(使用配置文件配置),使用 webpack 命令打包項目時,報了 ERROR in Entry module not found: Error: Can't resolve './src' in XXX 的錯誤。報錯截圖以及項目目錄如下:

img

在百度也找了找解決方案,有說 webpack 4.0 版本后可以不需要使用配置文件來打包項目(鏈接:https://www.cnblogs.com/tassel/p/12038104.html )。當時對這個解決方案的關鍵信息並不敏感,使用谷歌去搜索時,在github 中找到相應的解決方案。(鏈接:https://github.com/webpack/webpack/issues/6858 )。如圖:

image-20200524111246459

翻譯過來就是: 在 https://webpack.js.org/concepts/ 這個網址中寫道:“自從 v4.0.0 版本開始,webpack 不再需要配置文件”。

沒有配置文件,webpack 僅在存在 scr/index.js 文件時才有效。這個信息(使用粗體)在文檔中明確標明。

我們根據這條評論,點進這個網址,文本如下(如有需要可以自己翻譯或進入這個網址查看):

At its core, webpack is a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles.

Learn more about JavaScript modules and webpack modules here.

Since version 4.0.0, webpack does not require a configuration file to bundle your project. Nevertheless, it is incredibly configurable to better fit your needs.

由於我是使用配置文件來使用 webpack 打包項目的,所以我往下查找配置文件配置的官方描述:

Entry
An entry point indicates which module webpack should use to begin building out its internal dependency graph. webpack will figure out which other modules and libraries that entry point depends on (directly and indirectly).

By default its value is , but you can specify a different (or multiple entry points) by setting an entry property in the webpack configuration. For example:./src/index.js

並且有一段示例代碼:

webpack.config.js

module.exports = {
  entry: './path/to/my/entry/file.js'
};

然后我回去檢查我的項目結構,發現我配置文件的文件名寫錯了!(也太低級的錯誤了吧,捂臉.jpg)

官方中明確說明,如果使用配置文件使用 webpack 打包項目,則文件名是 webpack.config.js 。而我根據以前配置文件命名的習慣,寫了 webpack-config.js 。所以配置文件名中是 .config,而不是 -config ,修改下文件名重新打包即可。

我這里再總結一下我百度和谷歌搜到的解決方案的綜合吧!

解決方案

  1. 檢查 entry 配置的路徑是否出錯
  2. 檢查 webpack.config.js 的路徑是否出錯,webpack.config.js 必須是放在根目錄下
  3. 檢查配置文件文件名是否出錯,是 webpack.config.js 而不是 webpack-config.js
  4. 如果是 v4.0.0 以上版本的 webpack,可以不使用配置文件配置 webpack ,使用官網推薦的使用 ./src/index.js 作為入口點


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM