[英]React: Couldn't find preset “env react” relative to directory “src”
我已使用以下命令將預設 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(應用程序的文件夾名稱)
紗線鎖
現在當我運行命令時
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"
我遇到了同樣的問題,這看起來像是 Udemy 的 React 課程的一部分。
刪除所有與 Babel 相關的全局 NPM/Yarn 模塊。 在 Windows 上,我的在 c:\users\user\appdata\roaming\npm\nodemodules 中,因為它是全局安裝的。
npm 啟動項目
npm install --save-dev @babel/core @babel/cli
npm install --save-dev @babel/preset-react @babel/preset-env
npx babel.\src\app.js -o.\public\scripts\app.js --presets=@babel/preset-env,@babel/preset-react
首先,確保已經安裝了以下軟件包:
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-react
然后運行以下命令:
babel src/app.js --out-file=public/scripts/app.js --presets="env,react"
希望這可以幫助:)
只需使用代碼
babel src/app.js --out-file=public/scripts/app.js --presets='env,react'
首先,確保已經安裝了以下軟件包:
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-react
假設您安裝了上面的軟件包,在babel docs中,它聲明要使用預設,您應該執行以下操作:
--presets=@babel/preset-react,@babel/preset-env
所以你的 babel 命令應該是這樣的:
babel src/app.js --out-file=public/scripts/app.js --presets=@babel/preset-react,@babel/preset-env
希望這可以幫助!
我刪除了 bable 並使用全局重新安裝它
npm i -g @babel/cli.
然后我命令: yarn init
,在我生成package.json
文件的根文件夾中。
然后: yarn add @babel/preset-react @babel/preset-env
。 現在在 package.json 中添加了預設的依賴項。
然后我做了babel src/app.js --out-file=public/scripts/app.js --presets=@babel/preset-react,@babel/preset-env
我現在收到一個錯誤
internal/modules/cjs/loader.js:800
throw err;
^
Error: Cannot find module '@babel/core'
Require stack:
- C:\Users\anil\AppData\Roaming\npm\node_modules\@babel\cli\lib\babel\options.js
- C:\Users\anil\AppData\Roaming\npm\node_modules\@babel\cli\lib\babel\index.js
- C:\Users\anil\AppData\Roaming\npm\node_modules\@babel\cli\bin\babel.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
at Function.Module._load (internal/modules/cjs/loader.js:690:27)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at _core (C:\Users\anil\AppData\Roaming\npm\node_modules\@babel\cli\lib\babel\options.js:29:16)
at Object.<anonymous> (C:\Users\anil\AppData\Roaming\npm\node_modules\@babel\cli\lib\babel\options.js:138:76)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\anil\\AppData\\Roaming\\npm\\node_modules\\@babel\\cli\\lib\\babel\\options.js',
'C:\\Users\\anil\\AppData\\Roaming\\npm\\node_modules\\@babel\\cli\\lib\\babel\\index.js',
'C:\\Users\\anil\\AppData\\Roaming\\npm\\node_modules\\@babel\\cli\\bin\\babel.js'
]
}
我通過添加yarn add @babel/core
,但同樣的錯誤仍然存在
這對我有用。 刪除 node_modules 目錄中的所有 Babel。 然后,正如 motoXORx90 所說的前 3 個步驟。
npm 啟動項目
npm install --save-dev @babel/core @babel/cli
npm install --save-dev @babel/preset-react @babel/preset-env
最后一步:
npx babel src/app.js --out-file=public/scripts/app.js --presets=@babel/preset-env,@babel/preset-react
我遵循了上述答案,但仍然無法使其運行。 然后我注意到錯誤日志中寫的是什么。 聽從他們的建議,以下命令完美運行
npx babel .\src\app.js -o .\public\scripts\app.js --presets=module:@babel\preset-env,module:@babel\preset-react
我的 package.json
"@babel/cli": "^7.10.5",
"@babel/core": "^7.11.1",
"@babel/preset-env": "^7.11.0",
"@babel/preset-react": "^7.10.4"
對於那些正在研究 UDEMY 的反應課程並面臨此錯誤的人,這里有一個簡單的修復程序。 不要卸載任何東西......只需執行以下語句
babel src/app.js --out-file=public/scripts/app.js --presets='env,react'
而已
晚點再謝我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.