reactjs - react : Couldn't find preset "env react" relative to directory "src"

标签 reactjs babel-preset-env

我使用以下命令将预设 react 和 env 添加到我的 react 项目中:

yarn 全局添加 babel-preset-react@6.24.1 babel-preset-env@1.5.2

My package.json file has updated the presets and looks like the following:
  {
  "name": "indecesion-app",
  "version": "1.0.0",
  "main": "index.js",
  "author": "ak",
  "license": "MIT",
  "dependencies": {
    "babel-preset-env": "1.5.2",
    "babel-preset-react": "6.24.1"
  }
}

甚至我的 node_modules 文件夹也更新了预设。

文件夹结构如下所示:

indecesion-app(应用程序的文件夹名称)
  • node_modules
  • 公众号
  • package.json
  • yarn 锁

    现在当我运行命令

  • indecesion-app> babel src/app.js --out-file=public/scripts/app.js --presets=env,react

    它显示错误:
    Error: Couldn't find preset "env react" relative to directory "src"
    at C:\Users\anil\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:293:19
    at Array.map (<anonymous>)
    at OptionManager.resolvePresets (C:\Users\anil\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:275:20)
    at OptionManager.mergePresets (C:\Users\anil\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:264:10)
    at OptionManager.mergeOptions (C:\Users\anil\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:249:14)
    at OptionManager.init (C:\Users\anil\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\options\option-manager.js:368:12)
    at File.initOptions (C:\Users\anil\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\index.js:212:65)
    at new File (C:\Users\anil\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\file\index.js:135:24)
    at Pipeline.transform (C:\Users\anil\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-core\lib\transformation\pipeline.js:46:16)
    at transform (C:\Users\anil\AppData\Roaming\npm\node_modules\babel-cli\lib\babel\util.js:50:22)
    

    请提出解决方案

    最佳答案

    我遇到了同样的问题,只需添加引号即可解决:

    babel src/app.js --out-file=public/scripts/app.js --presets="env,react"

    关于reactjs - react : Couldn't find preset "env react" relative to directory "src",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61116908/

    相关文章:

    javascript - 汇总 + @babel/preset-env + @babel/polyfill

    reactjs - 错误 : Failed to load plugin import: Cannot find module 'eslint-plugin-import' when deploying a create-react-app

    webpack - 为什么使用 babel preset-env with `useBuiltIns: "usage"` 导致 harmony-module 出错?

    javascript - 当数据库更改时为每个人更新 DOM - MongoDB、Ajax、Express、React

    reactjs - 在 react-typescript 项目中创建后端 typescript 模块

    javascript - 在 Gatsby 中始终提供使用浏览器列表的 Polyfill

    cross-browser - 带有 babel-preset-env 的 Babel 似乎忽略了 browserslist 配置

    babeljs - 如何配置 babel/preset-env 以包含 core-js URLSearchParams polyfill?

    reactjs - 将 React 应用程序部署到 gitlab 页面后出现空白页面

    reactjs - 如何在 ASP.NET Core React/Redux 模板项目中更新我的 React 版本?