gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:37:14 28 4
gpt4 key购买 nike

当我执行 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
]
}

关于javascript - webpack 和 babel-polyfill : Can't resolve 'core-js/modules/es6.array.map' in source directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54869212/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com