使用 npm 运行 bash 脚本 [英] Running bash scripts with npm

查看:90
本文介绍了使用 npm 运行 bash 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使用 npm 为 Web 应用程序运行各种构建任务.我知道我可以通过将 scripts 字段添加到我的 package.json 来做到这一点,如下所示:

I want to try using npm to run my various build tasks for a web application. I know I can do this by adding a scripts field to my package.json like so:

"scripts": {
   "build": "some build command"
},

当您有更复杂的命令和一堆选项时,这会变得很笨拙.是否可以将这些命令移动到 bash 脚本或类似的东西?类似的东西:

This gets unwieldy when you have more complex commands with a bunch of options. Is it possible to move these commands to a bash script or something along those lines? Something like:

"scripts": {
   "build": "build.sh"
},

npm run build 会在哪里执行 build.sh 文件中的命令?

where npm run build would execute the commands in the build.sh file?

通读这篇帖子似乎是这样,但我不清楚我到底在哪里应该删除我的 build.sh 文件,或者如果我遗漏了什么.

Reading through this post it seems like it is, but I'm not clear on exactly where I'm supposed to drop my build.sh file or if I'm missing something.

推荐答案

完全有可能...

"scripts": {
   "build": "./build.sh"
},

另外,请确保在 bash 文件的顶部放置一个 hash bang #!/usr/bin/env bash

also, make sure you put a hash bang at the top of your bash file #!/usr/bin/env bash

还要确保你有权限执行文件

also make sure you have permissions to execute the file

chmod +x ./build.sh

最后,在 npm 中运行构建的命令是

Finally, the command to run build in npm would be

npm run build

这篇关于使用 npm 运行 bash 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆