如何将自定义脚本添加到运行javascript文件的package.json文件中
How do I add a custom script to my package.json file that runs a javascript file?
我希望能够在将运行node script1.js
的项目目录中执行命令script1
。
script1.js
是同一目录中的一个文件。该命令需要特定于项目目录,这意味着如果我向其他人发送项目文件夹,他们将能够运行相同的命令。
到目前为止,我已经尝试添加:
"scripts": {
"script1": "node script1.js"
}
到我的package.json文件,但当我尝试运行script1
时,我会得到以下输出:
zsh: command not found: script1
有人知道将上面提到的脚本添加到项目文件夹所需的步骤吗?
*注意:该命令不能添加到bash配置文件中(不能是特定于机器的命令)
如果你需要澄清,请告诉我。
npm run-script <custom_script_name>
或
npm run <custom_script_name>
在您的示例中,您希望运行npm run-script script1
或npm run script1
。
请参阅https://docs.npmjs.com/cli/run-script
生命周期脚本
Node还允许您为某些生命周期事件运行自定义脚本,例如在运行npm install
之后。这些可以在这里找到。
例如:
"scripts": {
"postinstall": "electron-rebuild",
},
这将在npm install
命令之后运行electron-rebuild
。
我已经创建了以下内容,它正在我的系统上运行。请尝试这个:
package.json:
{
"name": "test app",
"version": "1.0.0",
"scripts": {
"start": "node script1.js"
}
}
script1.js:
console.log('testing')
从命令行运行以下命令:
npm start
附加用例
我的package.json文件通常有以下脚本,这些脚本使我能够查看我的文件的typescript、sass编译以及运行服务器。
"scripts": {
"start": "concurrently '"sass --watch ./style/sass:./style/css'" '"npm run tsc:w'" '"npm run lite'" ",
"tsc": "tsc",
"tsc:w": "tsc -w",
"lite": "lite-server",
"typings": "typings",
"postinstall": "typings install"
}
步骤如下:
在package.json中添加:
"bin":{ "script1": "bin/script1.js" }
在项目目录中创建一个
bin
文件夹,并添加文件runScript1.js
,代码为:#! /usr/bin/env node var shell = require("shelljs"); shell.exec("node step1script.js");
在终端中运行
npm install shelljs
在终端中运行
npm link
从终端,您现在可以运行
script1
,它将运行node script1.js
参考:http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm
比方说,在脚本中,你想用一个命令运行两个命令:
"scripts":{
"start":"any command",
"singleCommandToRunTwoCommand":"some command here && npm start"
}
现在转到您的终端并在那里运行npm run singleCommandToRunTwoCommand
。
假设我的"package.json";
"scripts": {
"test": "echo '"Error: no test specified'" && exit 1",
"export_advertisements": "node export.js advertisements",
"export_homedata": "node export.js homedata",
"export_customdata": "node export.js customdata",
"export_rooms": "node export.js rooms"
},
现在运行脚本";export_advertiments";,我会简单地去终端并键入
npm run export_advertisements
示例:
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"build_c": "ng build --prod && del '"../../server/front-end/*.*'" /s /q & xcopy /s dist '"../../server/front-end'"",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
正如您所看到的,脚本"build_c"正在构建angular应用程序,然后从目录中删除所有旧文件,最后复制结果构建文件。
在我的情况下,我太愚蠢了,我正在运行以下命令
node run build
而不是低于命令
npm run build
请在清理并重新运行安装之前重新检查您的命令一次。
此外,请不要忘记npx,您可以在不安装它的情况下使用任何模块。
- Word文件(Javascript?)中有哪些字符
- 获取二进制数据并将其保存为.mp3文件Javascript
- 创建包含文本内容的新 Google 云端硬盘文件 (javascript)
- 将从 URL 输出的 JSON 保存到文件 javascript
- 单个页面上的多个音频文件javascript
- 将表单数据写入json文件-javascript
- imacros提取两部分数据+连接常量保存到文件javascript
- 检索 XML 文件 (JavaScript)
- 如何将画布图像“data:image/jpeg;base64,.”转换为普通图像文件 - JavaScript
- 用固定的行名称附加文件 javascript
- 打开并读取二进制文件(JavaScript)
- 将 emacs 用于大型多文件 JavaScript 项目
- 加载外部javascript,但HTTP请求重复一个文件javascript
- 从2D数组创建图像文件-JavaScript
- 正在读取文本文件Javascript
- 通过django将STATIC_URL传递到文件javascript
- 从输入文件javascript中获取一些数据
- Django”;静态“;文件-Javascript
- 上传两次相同的图像文件-Javascript
- 从文本文件javascript中读取行/变量