找不到模块babel-preset-es2015

时间:2018-10-19 09:07:32

标签: node.js reactjs

再次遇到相同的错误:Error: Cannot find module 'babel-preset-es2015'

完整错误日志:

ERROR in ./main.js Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-preset-es2015' from 'C:\Users\hp\Desktop\reactApp'
 at Function.module.exports [as sync] (C:\Users\hp\Desktop\reactApp\node_modules\resolve\lib\sync.js:43:15)
 at resolveStandardizedName (C:\Users\hp\Desktop\reactApp\node_modules\@babel\core\lib\config\files\plugins.js:101:31)
 at resolvePreset (C:\Users\hp\Desktop\reactApp\node_modules\@babel\core\lib\config\files\plugins.js:58:10)
 at loadPreset (C:\Users\hp\Desktop\reactApp\node_modules\@babel\core\lib\config\files\plugins.js:77:20)
 at createDescriptor (C:\Users\hp\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:154:9)
 at items.map (C:\Users\hp\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:109:50)
 at Array.map ()
 at createDescriptors (C:\Users\hp\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:109:29)
 at createPresetDescriptors (C:\Users\hp\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:101:10)
 at passPerPreset (C:\Users\hp\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:58:96) @ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./main.js main[2]

3 个答案:

答案 0 :(得分:30)

对于Babel版本6,软件包名称为babel-preset-es2015,对于Babel版本7,软件包名称为@babel/preset-es2015

从该错误看来,您正在使用版本7。es20XX-presets已过时,因此建议您切换到@babel/preset-env

首先安装预设(使用npm):

npm install --save-dev @babel/preset-env

然后将预设添加到您的.babelrc

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

答案 1 :(得分:1)

错误日志明确指出它找不到模块。 Cannot find module 'babel-preset-es2015' from 'C:\Users\hp\Desktop\reactApp' at Function.module.exports [as sync] 只需运行此命令即可安装软件包

npm install --save-dev @babel/preset-es2015

还要检查您的.babelrc

更多信息在这里-> https://babeljs.io/docs/en/babel-preset-es2015

答案 2 :(得分:0)

通行证7使用@babel/preset-env。其他预设已弃用 这里说一下https://babeljs.io/docs/en/v7-migration#yearly-preset-deprecations-blog-2017-12-27-nearing-the-70-releasehtml-deprecated-yearly-presets-eg-babel-preset-es20xx

您应该在配置文件(我不知道您是否正在使用Webpack或其他)中指定预设为“ @ babel / preset-env”。通过npm install --save-dev @babel/preset-env

安装

例如,您正在使用Webpack模块捆绑器。在这种情况下,请这样指定:

use: {
      loader: "babel-loader",
      options: {
                presets: ["@babel/preset-env"]
      }
}

如果您突然需要,这里是https://webpack.js.org/loaders/babel-loader/文档。

编码愉快!