webpack & babel-polyfill:无法在源目录中解析 'core-js/modules/es6.array.map'

webpack & babel-polyfill: Can't resolve 'core-js/modules/es6.array.map' in source directory

我在执行 webpack:

时遇到这个错误
Module not found: Error: Can't resolve 'core-js/modules/es6.array.map' in '/path/to/project/src'
 @ ./src/index.ts 1:0-39

index.ts:

console.log([1, 2, 3].map(x => x * x));

.babelrc:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "usage"
      }
    ]
  ]
}

webpack.config.js:

const path = require('path');

module.exports = {
  mode: 'development',

  entry: './src/index.ts',
  devtool: false,
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: [
          { loader: 'babel-loader' },
          { loader: 'ts-loader' }
        ]
      }
    ]
  },
  resolve: {
    extensions: [
      '.ts'
    ]
  }
};

我觉得很奇怪错误是试图解析 src/ 中的模块,而不是 node_modules/

我尝试删除 node_modulespackage.json,然后删除 npm i,但情况并没有改变。

我也试过另一种方法来使用 @babel/polyfill 描述 here

useBuiltIns设置为'entry'只是增加了类似错误的数量。将其设置为 false 会导致不同的错误。

Module not found: Error: Can't resolve 'core-js/es6' in '/path/to/project/node_modules/@babel/polyfill/lib'
 @ ./node_modules/@babel/polyfill/lib/index.js 3:0-22
 @ multi @babel/polyfill ./src/index.ts

ERROR in ./node_modules/@babel/polyfill/lib/index.js
Module not found: Error: Can't resolve 'regenerator-runtime/runtime' in '/path/to/project/node_modules/@babel/polyfill/lib'
 @ ./node_modules/@babel/polyfill/lib/index.js 23:0-38
 @ multi @babel/polyfill ./src/index.ts

core-jsregenerator-runtime 似乎应该安装在 node_modules/@babel/polyfill/ 中。目前,目录中只有 index.jsnoConflict.js。我是否必须在此目录中手动安装这些模块?

尝试在您的 resolve 中添加以下内容 -

  resolve: {
    extensions: [
      '.ts',
      '.js' // add this
    ]
  }