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

查看:1590
本文介绍了使用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文件顶部放置一个哈希爆炸#!/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中运行build的命令将是

Finally, the command to run build in npm would be

npm run build

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

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