Search code examples
javascriptnode.jstypescriptwebpackcore-js

Webpack cannot resolve core-js paths on Windows


Running webpack I receive the errors:

ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './es' in 'pathtoproject\node_modules\core-js'
 @ ./node_modules/core-js/index.js 1:0-15
 @ multi core-js whatwg-fetch ./src/contactForm.tsx

ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './internals/path' in 'pathtoproject\node_modules\core-js'
 @ ./node_modules/core-js/index.js 4:11-38
 @ multi core-js whatwg-fetch ./src/contactForm.tsx

ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './proposals' in 'pathtoproject\node_modules\core-js'
 @ ./node_modules/core-js/index.js 2:0-22
 @ multi core-js whatwg-fetch ./src/contactForm.tsx

ERROR in ./node_modules/core-js/index.js
Module not found: Error: Can't resolve './web' in 'pathtoproject\node_modules\core-js'
 @ ./node_modules/core-js/index.js 3:0-16
 @ multi core-js whatwg-fetch ./src/contactForm.tsx

The folders do exist though. I believe it's to do with Windows paths as when I change the core-js/index.js file to use path.resolve() instead of relative imports it does work. This is not the solution I want though as it requires altering the core-js module.

For reference here is my webpack.config.js

const path = require('path');

module.exports = {
    entry: ["core-js", "whatwg-fetch", "./src/contactForm.tsx"],

    output: {
        path: path.join(__dirname, "build/assets/js/"),
        filename: "contactForm.js"
    },

    mode: "production",

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx"]
    },

    module: {
        rules: [
            {
                test: /\.ts(x?)$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: "ts-loader"
                    }
                ]
            },
            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            {
                enforce: "pre",
                test: /\.js$/,
                loader: "source-map-loader"
            }
        ]
    },

    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    }
};

Any ideas of a fix would be greatly appreciated


Solution

  • The reason for this was because I did not have javascript files as a resolvable extension in my webpack config.

    ...
    resolve: {
            //modules: ['node_modules'],
            // Add '.ts' and '.tsx' as resolvable extensions.
            extensions: [".ts", ".tsx"]
        },
    ...
    

    Needed to be

    ...
    resolve: {
            //modules: ['node_modules'],
            // Add '.ts' and '.tsx' as resolvable extensions.
            extensions: [".ts", ".tsx", ".js"]
        },
    ...