【发布时间】: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