简体   繁体   English

在没有 yarn/npm 命令的情况下使用 package.json 脚本

[英]Use package.json script without yarn/npm command

Is there a possibility to run scripts commands inside package.json file without having to type yarn or npm before?是否有可能在package.json文件中运行scripts命令,而无需输入yarnnpm之前? I mean I have something like:我的意思是我有类似的东西:

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

so to run it I have to type yarn get [arguments] .所以要运行它,我必须输入yarn get [arguments] Is there any way to type only get [arguments] and it will also work?有没有办法只输入get [arguments]并且它也可以工作?

I believe these scripts are run off on npm / yarn shell-scripts, so you do need to call npm / yarn for it to know what to run.我相信这些脚本是在 npm / yarn shell-scripts 上运行的,所以你需要调用 npm / yarn 让它知道要运行什么。

Alternatively, you could extract these scripts out to a bash (.sh) file and execute the bash file.或者,您可以将这些脚本提取到 bash (.sh) 文件并执行 bash 文件。 In your package.json, you can also link the script to just run the bash file.在您的 package.json 中,您还可以链接脚本以仅运行 bash 文件。

if you are not on windows this wont work but i am so im not sure how to do on any other os如果您不在 windows 上,这将不起作用,但我不确定如何在任何其他操作系统上执行
In windows you can just create a.cmd file called eg.在 windows 中,您可以创建一个名为例如的.cmd 文件。 "get.cmd" then in command prompt run “get.cmd”然后在命令提示符下运行
what it does is just run the file named "get" and it asks for the args then runs the file;它所做的只是运行名为“get”的文件并询问参数然后运行该文件; it does not need the extension so its just "get"它不需要扩展,所以它只是“获取”
"get" and it will run the content inside the file “get”,它将运行文件中的内容

package.json: package.json:

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

get.cmd: (this is windows batch) 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: (this is just for example put your real file) index.js:(这只是例如放置您的真实文件)

console.log("you ran index!)

In command prompt:在命令提示符下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM