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

时间:2019-02-25 15:10:33

标签: javascript node.js typescript webpack babel-polyfill

执行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,但这并没有改变情况。

我还尝试了另一种使用here中所述的@babel/polyfill的方式。

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。我必须在这些目录中手动安装那些模块吗?

1 个答案:

答案 0 :(得分:1)

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

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