npm 构建报 Module not found:导入路径被视为 fully specified

March 27, 2024

构建时可能会遇到这条报错

Module not found: Error: Can't resolve './xxx' in '/yourprojectdirectory/src' Did you mean 'xxx.js'? BREAKING CHANGE: The request './xxx' failed to resolve only because it was resolved as fully specified

一种解决办法:修改模块解析规则

{
    test: /\.m?js/,
    resolve: {
      fullySpecified: false,
    },
}

也可以给缺少扩展名的导入路径加上 .js

import xxx from './yourprojectdirectory/src/xxx';

// 改成:
import xxx from './yourprojectdirectory/src/xxx.js';
  1. JahnoelRondon(2022 年 1 月 1 日),“type”: “module” with React,GitHub issue #11865。涉及 JavaScript MIME 类型、.mjs,以及 package.json 声明 "type": "module" 的 .js 文件。https://github.com/facebook/create-react-app/issues/11865

  2. user3130985(2022 年 2 月 3 日),关于 Create React App 中 Webpack 5 无法解析非完整路径的提问,Stack Overflow。https://stackoverflow.com/questions/70964723/webpack-5-in-ceate-react-app-cant-resolve-not-fully-specified-routes

  3. puppe82(2022 年 12 月 28 日),Webpack 5: Import fails to resolve with Module not found error,GitHub issue #7114。https://github.com/fullcalendar/fullcalendar/issues/7114

© 2024 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2024-03-27). npm 构建报 Module not found:导入路径被视为 fully specified. https://caiborui.com/blog/2024-03-27-en-npm-building-error-failed-to-resolve-zhtrans

@misc{borui2024-ca592db7,
  author = {borui},
  title = {{npm 构建报 Module not found:导入路径被视为 fully specified}},
  year = {2024},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2024-03-27-en-npm-building-error-failed-to-resolve-zhtrans}
}