Webpack cannot resolve /index.(js|jsx) files when package is type module

99 Views Asked by At

I'm pretty new to Webpack configuration and difference between cjs, mjs etc. When trying to build a component lib bundled by Webpack, im coming across this error when building (webpack command)

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.

When adding index.js to ./components/ its works well, but I want my webpack to be able to resolve it by itself. Note that im using package.json type:module to use import syntex My webpack config:

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"],
            },
          },
        ],
      },
    ],
  },
};

1

There are 1 best solutions below

0
On

Solve it by adding resolve: { fullySpecified: false }, to the babel rule in rules section