【问题标题】:Use package.json script without yarn/npm command使用没有 yarn/npm 命令的 package.json 脚本
【发布时间】:2021-05-26 07:45:43
【问题描述】:

是否有可能在 package.json 文件中运行 scripts 命令而无需之前键入 yarn 或 npm?我的意思是我有类似的东西:

 "scripts": {
    "get": "node index.js"
  }

所以要运行它,我必须输入yarn get [arguments]。有什么方法可以只输入get [arguments] 也可以吗?

【问题讨论】:

  • 它只是一个 json 文件。您可以通过任何方式阅读它。蛋。 stedolan.github.io/jq,或来自诸如节点或 python 之类的程序。然后您可以将该命令通过管道传输到 shell 中。
  • 您似乎想将模块安装为全局和use the bin field
  • 它基本上只是我想通过终端的简单命令运行的 index.js 文件

标签: javascript node.js npm yarnpkg


【解决方案1】:

我相信这些脚本是在 npm / yarn shell-scripts 上运行的,所以你需要调用 npm / yarn 让它知道要运行什么。

或者,您可以将这些脚本提取到 bash (.sh) 文件中并执行该 bash 文件。在您的 package.json 中,您还可以链接脚本以仅运行 bash 文件。

【讨论】:

  • 它基本上只是我想通过终端的简单命令运行的 index.js 文件
  • 对于您的简单情况 (node index.js) 这应该没问题,但值得注意的是,运行 npm run get 也会隐式地将 ./node_modules/.bin 附加到您的 $PATH,所以如果您将其拉出一个单独的 shell 脚本,你可能也想这样做。
【解决方案2】:

如果您不在 Windows 上,这将无法正常工作,但我不确定如何在任何其他操作系统上进行操作
在 Windows 中,您可以创建一个名为 eg 的 .cmd 文件。 “get.cmd”然后在命令提示符下运行
它所做的只是运行名为“get”的文件并询问参数然后运行该文件;它不需要扩展,所以它只是“获取”
“get”,它将运行文件中的内容

package.json:

"scripts": {
   "get": "node index.js"
}

get.cmd:(这是windows批处理)

# just to make it cleaner (you dont get it showing what was ran it just runs)
@echo off
# this is to get the args
set /p args="args: "
# then it runs the yarn with %args% because args was defined with the set /p
yarn get %args%

index.js: (这只是放你真实文件的例子)

console.log("you ran index!)

在命令提示符下:

C:\users\*user*\*folder*> get
args: 123
you ran index!

【讨论】:

    猜你喜欢
    • 2017-05-29
    • 2020-07-27
    • 2023-04-08
    • 1970-01-01
    • 2018-03-31
    • 2022-08-17
    • 2020-05-09
    相关资源
    最近更新 更多