gpt4 book ai didi

reactjs - 在 React 和 Webpack 项目上从 "babel-preset-es2015"转换到 "babel-preset-env"时出现问题

转载 作者:行者123 更新时间:2023-12-04 03:08:55 33 4
gpt4 key购买 nike

我有一个一直在使用 babel-preset-es2015 的 React 项目,它使用 webpack 构建得很好,但是自从我搬到了 babel-preset-env 模块构建失败。

出现此错误消息:

ERROR in ./src/index.js
Module build failed: Error: Couldn't find preset "es2015" relative to directory "/path/to/project"
at /path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
at Array.map (<anonymous>)
at OptionManager.resolvePresets (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
at OptionManager.mergePresets (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
at OptionManager.mergeOptions (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
at OptionManager.init (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
at File.initOptions (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:212:65)
at new File (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:135:24)
at Pipeline.transform (/path/to/project/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
at transpile (/path/to/project/node_modules/babel-loader/lib/index.js:50:20)
at Object.module.exports (/path/to/project/node_modules/babel-loader/lib/index.js:175:20)

以下是我的 webpack 配置:

工作(使用 babel-preset-2015)

module.exports = {
...
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
'es2015',
'react',
'stage-1']
}
}
}
]
},
...
};

不起作用(使用 babel-preset-env)

module.exports = {
...
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
'env',
'react',
'stage-1']
}
}
}
]
},
...
};

package.json 依赖:

"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0"

最佳答案

所以我添加了一个 .babelrc 文件并将加载器选项移到那里,现在一切似乎都正常了。仍然不确定为什么它在 webpack 模块中不起作用。

网页包

module.exports = {
...
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
}
]
},
...
};

.babelrc

{
"presets": [
"env",
"react",
"stage-1"
]
}

关于reactjs - 在 React 和 Webpack 项目上从 "babel-preset-es2015"转换到 "babel-preset-env"时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719793/

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