【问题标题】:Webpack cannot resolve core-js paths on WindowsWebpack 无法解析 Windows 上的 core-js 路径
【发布时间】:2020-07-08 11:25:27
【问题描述】:

运行 webpack 我收到错误:

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

这些文件夹确实存在。我相信这与 Windows 路径有关,因为当我将 core-js/index.js 文件更改为使用 path.resolve() 而不是相对导入时,它确实有效。这不是我想要的解决方案,因为它需要更改 core-js 模块。

参考这里是我的 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"
    }
};

任何修复的想法将不胜感激

【问题讨论】:

    标签: javascript node.js typescript webpack core-js


    【解决方案1】:

    你可以尝试替换

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

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

    Path join 只是连接路径,resolve 会将路径视为绝对路径。

    您也可以添加上下文属性:

    module.exports = {
        context: path.resolve(__dirname),
    
        entry: ["core-js", "whatwg-fetch", "./src/contactForm.tsx"],
    

    【讨论】:

    • 抱歉,这不起作用。我认为它确实如此,但我之前删除了 core-js 作为入口点。
    【解决方案2】:

    这是因为我的 webpack 配置中没有 javascript 文件作为可解析的扩展。

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

    需要

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

    【讨论】:

      猜你喜欢
      • 2017-12-15
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      相关资源
      最近更新 更多