当包类型为模块时,Webpack 无法解析 /index.(js|jsx) 文件

问题描述 投票:0回答:1

我对 Webpack 配置以及 cjs、mjs 等之间的区别还很陌生。 当尝试构建由 Webpack 捆绑的组件库时,我在构建时遇到此错误(

webpack
命令)

BREAKING CHANGE: The request './components' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

index.js
添加到
./components/
时效果很好,但我希望我的 webpack 能够自行解决它。 请注意,我使用 package.json
type:module
来使用
import
syntex 我的 webpack 配置:

import path from "path";
/**
 * @type {import('webpack').Configuration}
 */
export default {
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(process.cwd(), "./dist"),
  },
  resolve: {
    extensions: [".js", ".jsx"],
    fullySpecified: false,
  },
  mode: "development",
  devtool: "source-map",
  module: {
    rules: [
      {
        type: "javascript/auto",
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: ["@babel/preset-env", "@babel/preset-react"],
            },
          },
        ],
      },
    ],
  },
};

javascript webpack bundler package.json mjs
1个回答
0
投票

通过添加来解决

resolve: { fullySpecified: false },
rules
部分

中的babel规则
© www.soinside.com 2019 - 2024. All rights reserved.