Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

281
Vistas
Programmatic Webpack & Jest (ESM): can't resolve module without '.js' file extension

I'm using webpack programmatically, with typescript, ESM, and jest. In a jest test I'm getting errors for not including a .js file extension when importing ES modules. For example:

    Module not found: Error: Can't resolve 'modulename' in '/path/components'
    Did you mean 'modulename.js'?
    BREAKING CHANGE: The request 'modulename' 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.

The module in question does indeed have "type": "module" set in its package.json. I have tried adding .js to the import, and it doesn't help.

I'm invoking jest with:

node --experimental-vm-modules --experimental-specifier-resolution=node node_modules/jest/bin/jest.js

as is recommended in the docs (everything else works except webpack). Note that I have tried with and without --experimental-specifier-resolution=node (this has helped in other similar circumstances).

Any thoughts on how to get webpack to work? Thanks in advance!

Note: everything was working until it was all converted to ESM! Now only programmatic webpack isn't working.

Webpack config:

  {
    entry,
    target: 'web',
    output: {
      path: outputDir,
      filename: '[name].js',
    },
    mode: process.env.NODE_ENV as 'development' | 'production' | 'none' | undefined,
    resolve: {
      extensions: [
        '.ts',
        '.tsx',
        '.js',
        '.jsx',
        '.ttf',
        '.eot',
        '.otf',
        '.svg',
        '.png',
        '.woff',
        '.woff2',
        '.css',
        '.scss',
        '.sass',
      ],
    },
    module: {
      rules: [
        {
          test: /\.(ttf|eot|otf|svg|png)$/,
          loader: 'file-loader',
        },
        {
          test: /\.(woff|woff2)$/,
          loader: 'url-loader',
        },
        {
          test: /\.(js|jsx|ts|tsx)$/,
          exclude: /node_modules/,
          loader: 'babel-loader',
          options: {
            sourceType: 'unambiguous',
            presets: [
              [
                '@babel/preset-env',
                {
                  corejs: '3.0.0,',
                  useBuiltIns: 'usage',
                },
              ],
              '@babel/preset-react',
              '@babel/preset-typescript',
            ],
            plugins: [
              'css-modules-transform',
              [
                'babel-plugin-react-scoped-css',
                {
                  include: '.scoped.(sa|sc|c)ss$',
                },
              ],
              '@babel/plugin-proposal-class-properties',
            ],
          },
        },
        {
          test: /\.(sc|c|sa)ss$/,
          use: [
            'style-loader',
            'css-loader',
            'scoped-css-loader',
            'sass-loader',
          ],
        },
      ],
    },
  }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

@nerdlinger answer worked for me. I had this webpack.config.js

module.exports = {
    entry: './src/index.js',
    module: {
        rules: [
            {
                test: /\.(js)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            }
        ],
    }
    ...
}

and i changed it to this

module.exports = {
    entry: './src/index.js',
    module: {
        rules: [
            {
                test: /\.(js)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                },
                resolve: {
                  fullySpecified: false,
                }
            }
        ],
    }
    ...
}
over 4 years ago · Santiago Trujillo Denunciar

0

Ok so I found the solution here.

Basically, had to add 2 things to the webpack config under module.rules:

{
  test: /\.m?js/,
  type: "javascript/auto",
},
{
  test: /\.m?js/,
  resolve: {
    fullySpecified: false,
  },
},
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda