babel 命令在终端中 运行 时转换,但 运行ning 脚本以错误结束

babel command converts when run in terminal but running the script ends in error

我一直在尝试让 babel 核心和 cli 将现代 JavaScript 从 index.js 转换为 bundle.js。

这是我的简单 package.json

{
    "name": "chapter19",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "babel": "./node_modules/.bin/babel ./src/index.js -w -o ./dist/assets/bundle.js"
    },
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "@babel/cli": "^7.16.7",
        "@babel/core": "^7.16.7",
        "@babel/preset-env": "^7.16.7",
        "babel-loader": "^8.2.3"
    }
}

现在当我在终端中 运行 node_modules/.bin/babel ./src/index.js -w -o ./dist/assets/bundle.js 时,它工作正常但是当我尝试在终端中 运行 "npm 运行 babel" 时,我得到以下信息错误。到目前为止,这是我尝试过的:

"babel": "node_modules/.bin/babel src/index.js -w -o dist/assets/bundle.js"

结果:'node_modules' 未被识别为内部或外部命令, 可运行的程序或批处理文件。

"babel": "./node_modules/.bin/babel src/index.js -w -o dist/assets/bundle.js"

结果:'.'不被识别为内部或外部命令, 可运行的程序或批处理文件。

我也看过期,基本上就是:

"babel": "node ./node_modules/.bin/babel src/index.js -w -o dist/assets/bundle.js"

"babel": "node ./node_modules/.bin/babel ./src/index.js -w -o ./dist/assets/bundle.js"

我收到以下错误:

SyntaxError: missing ) after argument list
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

这是我的文件夹结构(以备不时之需)

对于那些有兴趣解决这个问题的人,在 Windows 你可以使用以下内容:

"scripts": {
    "babel": ".\node_modules\.bin\babel src\index.js -w -o dist\assets\bundle.js"
},