question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compilation errors when building with webpack 5

See original GitHub issue

Hi @fernandopasik, thanks for your project, it’s pretty useful. We are using your code in order to traverse React trees in runtime and do some stuff to adapt dynamically how a chat must be shown depending on a messaging provider (Facebook, Whatsapp, …).

Expected behavior

It should work correctly with Webpack 5.9.0. Maybe transpiled lib should preserve the file extension in order to not break with webpack.

Current behavior

We are now trying to migrate our projects to Webpack 5 and we have experienced the following kind of errors:

ERROR in ./node_modules/react-children-utilities/react-children-utilities.js 10:0-38
Module not found: Error: Can't resolve './lib/onlyText' in '/.../.../.../.../botonic-examples/blank/node_modules/react-children-utilities'
Did you mean 'onlyText.js'?
BREAKING CHANGE: The request './lib/onlyText' failed to resolve only because it was resolved as fully specified
(probably because the origin is 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.

Steps to reproduce

  1. Init a project with Webpack 5.9.0.
  2. Configure babel (for example):
const babelLoaderConfig = {
  exclude: /node_modules[\/\\](?!(@botonic)[\/\\])/,
  use: {
    loader: 'babel-loader',
    options: {
      cacheDirectory: true,
      presets: ['@babel/preset-env', '@babel/react'],
      plugins: [
        '@babel/plugin-proposal-object-rest-spread',
        '@babel/plugin-proposal-class-properties',
        'babel-plugin-add-module-exports',
        '@babel/plugin-transform-runtime',
      ],
    },
  },
}
  1. Try to compile the project with webpack.

Device information

  • OS: MacOs
  • Version: Node v12.20.0

Possible solution

If not fully necessary, do not specify type module in package.json. Otherwise, publish lib with it’s corresponding extensions. Related issues (solutions proposed didn’t work for me): https://github.com/graphql/graphql-js/issues/2721

In fact, changing manually the following code inside node_modules fixed the issue:

import { Children, isValidElement } from 'react';
import hasChildren from './hasChildren';

to

import { Children, isValidElement } from 'react';
import hasChildren from './hasChildren.js';

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
vanbasten17commented, Dec 3, 2020

Have you tried to just run babel before publishing the lib with this babel plugin? I managed to have this working by just applying this rule, but I think that the code must be shipped in order to be used without having problems. https://www.npmjs.com/package/babel-plugin-add-import-extension Example:

const babelLoaderConfig = {
  test: /\.?js/,
  exclude: /node_modules[\/\\](?!(react-children-utilities)[\/\\])/,
  use: {
    loader: 'babel-loader',
    options: {
      cacheDirectory: true,
      presets: ['@babel/preset-env', '@babel/react'],
      plugins: ['babel-plugin-add-import-extension'],
    },
  },
}
0reactions
fernandopasikcommented, Feb 25, 2021

Sorry for the delayed solution for this. I’ve added a task to have the .js extensions in the built files. This should work fine with the latest release v2.4.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compile error with Webpack 5 after upgrading but working ...
I am working with codesandbox.io and everything was fine until today. I realised that 4.6.3 compiles, but not 4.6.4 and 4.6.2 and lower....
Read more >
React Projekt!! Webpack 4 -> Webpack 5 == compiler error ...
i get some errors, about the Webpack compiler that the loaders can´t get readed. I tried to change and update the loaders but...
Read more >
Webpack 5 errors | ImmutableX Documentation
The reason for this error is that create-react-app uses a version of webpack greater than 5, which, unlike versions < 5, does not...
Read more >
Make Webpack exit on compilation errors - Medium
To instruct Webpack to exit on compilation errors, we introduced a simple custom plugin into our webpack. config. js.
Read more >
Stats - webpack
'minimal', none, Only output when errors or new compilation happen ... Tells stats whether to add the build date and the build time...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found