如何将自定义脚本添加到运行javascript文件的package.json文件中? [英] How do I add a custom script to my package.json file that runs a javascript file?

查看:765
本文介绍了如何将自定义脚本添加到运行javascript文件的package.json文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在项目目录中执行命令 script1 ,该目录将运行 node script1.js

I want to be able to execute the command script1 in a project directory that will run node script1.js.

script1.js 是同一目录中的文件。该命令需要特定于项目目录,这意味着如果我向其他人发送项目文件夹,他们将能够运行相同的命令。

script1.js is a file in the same directory. The command needs to be specific to the project directory, meaning that if I send someone else the project folder, they will be able to run the same command.

到目前为止,我尝试添加:

So far I've tried adding:

"scripts": {
    "script1": "node script1.js"
}

到我的package.json文件,但是当我尝试运行 script1 时,我得到以下输出:

to my package.json file but when I try running script1 I get the following output:

zsh: command not found: script1

有谁知道添加上面提到的脚本到项目文件夹?

Does anyone know the steps necessary to add the script mentioned above to the project folder?

*注意:命令无法添加到bash配置文件中(不能是特定于机器的命令)

*Note: the command can not be added to the bash profile (cannot be a machine specific command)

如果您需要任何澄清,请与我们联系。

Please let me know if you need any clarification.

推荐答案



自定义脚本



npm run-script< custom_script_name>

npm run< custom_script_name>

在您的示例中,您需要运行 npm run-script script1 npm run script1

In your example, you would want to run npm run-script script1 or npm run script1.

请参阅 https://docs.npmjs.com/cli/run-script

节点还允许您为某些生命周期事件运行自定义脚本,例如在<$ c之后运行$ c> npm install 。这些可以在这里找到。

Node also allows you to run custom scripts for certain lifecycle events, like after npm install is run. These can be found here.

例如:

"scripts": {
    "postinstall": "electron-rebuild",
},

这将运行电子重建 npm install 命令之后。

这篇关于如何将自定义脚本添加到运行javascript文件的package.json文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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