Skip to content

Module not found: Error: You attempted to import babel-preset which falls outside of the project src/ directory

I’m developing an application created using create-react-app

But then I needed to use mediainfojs library, this library requires wasm files, and based on what I understood I couldn’t add it using create-react-app, I had to eject it.

After ejecting it, I went to mediainfo information on how to add the wasm on the webpack

They use the CopyPlugin, but then when I tried to do that it complained about the versions of my webpack (4) and the CopyPlugin…. so, I decided to migrate to webpack 5

That is when the pain starts… after follow their migration tutorial and do a bunch of modifications on my webpack.config I got to the following error while runing yarn build:

Module not found: Error: You attempted to import /MyWorkspace/project/node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/asyncToGenerator which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.

The only place calling this babel-preset-react-app are in the configuation

Here:

                {
                    test: /.(js|mjs|jsx|ts|tsx)$/,
                    include: paths.appSrc,
                    loader: require.resolve("babel-loader"),
                    options: {
                        customize: require.resolve(
                            "babel-preset-react-app/webpack-overrides"
                        ),

And here:

                {
                    test: /.(js|mjs)$/,
                    exclude: /@babel(?:/|\{1,2})runtime/,
                    loader: require.resolve("babel-loader"),
                    options: {
                        babelrc: false,
                        configFile: false,
                        compact: false,
                        presets: [
                            [
                                require.resolve("babel-preset-react-app/dependencies"),
                                { helpers: true },
                            ],
                        ],
                        cacheDirectory: true,
                        cacheCompression: isEnvProduction,

                        // If an error happens in a package, it's possible to be
                        // because it was compiled. Thus, we don't want the browser
                        // debugger to show the original code. Instead, the code
                        // being evaluated would be much more helpful.
                        sourceMaps: false,
                    },
                },

I have looked into similar issues reported here, but mostly of them seem to be related to either static files being dynamically imported or imports referencing “..” dir after the project directory

The full webpack config file is here

I’m probably missing something very silly, I’d be glad if someone can point it out.

Answer

I’m also attempting to upgrade an ejected CRA project to Webpack 5. I was able to move forward using babel-preset-react-app-webpack-5, only to encounter the next CRA-related issue.

Be sure to replace calls like require.resolve("babel-preset-react-app/dependencies") with require.resolve("babel-preset-react-app-webpack-5/dependencies").

Also, be aware the package does not appear to be production-ready, but my own project is still in early development.