【问题标题】:Getting error "babel" is not recognized as an internal or external command,收到错误“babel”不被识别为内部或外部命令,
【发布时间】:2022-02-01 18:59:24
【问题描述】:

我有一个简单的 tests.js 文件,我希望使用源映射文件来跟进。我已尝试启动依赖项,但提示显示错误。如果有人能指出问题和解决方案将不胜感激^^

tests.js

var add = (...arr) => {
    return arr.reduce((sum, el) =>{
        return sum+el;
    }, 0)
}
console.log(add(1,2,3));

我一开始就尝试过这些命令

npm install -g babel-cli

npm install babel-preset-es2015

babel tests.js --out-file tests.dist.js --source-maps --presets=es2015

但收到同样的错误。我遵循了社区的另一个解决方案,但它仍然没有用。解决方案是删除 node_modules 并重新启动依赖项。

npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node

并在依赖项中添加"start": "nodemon --exec babel-node index.js",。 我检查了 node_modules 并且它也存在这些文件。

node_modules/.bin/babel-node

node_modules/.bin/babel-node.cmd - 仅限 Windows

node_modules/@babel/node/bin/babel-node.js

The solution I followed 不过,我还是不知道如何解决这个问题。这是我第一次使用 node 和 babel。我的节点版本是v16.13.1

已编辑

文件夹结构

Y:.
|   index.html
|   package-lock.json
|   package.json
|   tests.js
|   tree.txt
|   
\---node_modules
    |   .package-lock.json
    |   
    +---.bin
    |       babel
.....

好大啊!

package.json

{
  "dependencies": {
    "@babel/cli": "^7.16.8",
    "@babel/core": "^7.16.12",
    "@babel/preset-env": "^7.16.11"
  }
}

.babelrc

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

./node_modules/@babel/cli/bin/babel.js

require("../lib/babel");

使用 bash 命令后 -

./node_modules/@babel/cli/bin/babel.js example.js --out-file main.dist.js

错误

Error: Unknown option: .presents. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.
    at throwUnknownError (Y:\babel work\node_modules\@babel\core\lib\config\validation\options.js:133:27)
    at Y:\babel work\node_modules\@babel\core\lib\config\validation\options.js:118:5
    at Array.forEach (<anonymous>)
    at validateNested (Y:\babel work\node_modules\@babel\core\lib\config\validation\options.js:94:21)
    at validate (Y:\babel work\node_modules\@babel\core\lib\config\validation\options.js:85:10)
    at Y:\babel work\node_modules\@babel\core\lib\config\config-chain.js:209:34
    at cachedFunction (Y:\babel work\node_modules\@babel\core\lib\config\caching.js:60:27)
    at cachedFunction.next (<anonymous>)
    at evaluateSync (Y:\babel work\node_modules\gensync\index.js:251:28)
    at sync (Y:\babel work\node_modules\gensync\index.js:89:14) {
  code: 'BABEL_UNKNOWN_OPTION'
}

【问题讨论】:

  • 你是否设置了.babelrc 文件这样的配置{ "presets": [ "@babel/preset-env" ] }
  • 你能分享你的文件夹结构吗?
  • @cooskun 嗨,我已经添加了文件夹结构的一部分。它有 .bin@babel 以及 node_modules 中的很多东西。
  • @ullaskunder 嗨,我尝试了您发布的解决方案。添加了.babelrc 文件,但每当我选择/babel.js --version 时,它都会在babel.js 文件中显示require("../lib/babel");。怎么走?
  • @ullaskunder 非常感谢!我现在把所有的问题都整理好了谢谢你!

标签: javascript babeljs node-modules


【解决方案1】:

嘿嘿, [更新]

本地安装 babel

  • npm i @babel/core @babel/cli @babel/preset-env

  • 在package.json里面添加npm脚本

  "scripts": {
    "start-babel": "babel example.js --out-file main.dist.js"
  },
  • 所有配置都需要.babelrc 文件
// basic need
{
    "presets": [
        "@babel/preset-env"
    ]
}
  • npm run start-babel 运行脚本,这将创建main.dist.js js 转译文件

  • 你需要.babelrc文件和一些配置

  • 如果你在本地安装了@bable/core @babel/cli @bable/preset-env

    • 那你得像这样使用node_modules的babel.js的路径

  • 如果您使用 -g 全局安装它,那么您不需要路径,只需使用 babel

【讨论】:

    猜你喜欢
    • 2020-04-12
    • 2021-04-25
    • 2018-11-01
    • 1970-01-01
    • 2022-01-07
    • 2021-09-15
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多