我希望能够script1
在将要运行的项目目录中执行命令node script1.js
。
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",
},
这将electron-rebuild
在npm install
命令后运行。
我创建了以下内容,并且可以在我的系统上正常工作。请尝试以下方法:
package.json:
{
"name": "test app",
"version": "1.0.0",
"scripts": {
"start": "node script1.js"
}
}
script1.js:
console.log('testing')
在命令行中运行以下命令:
npm start
附加用例
我的package.json文件通常具有以下脚本,这些脚本使我能够查看文件的打字稿,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
假设您要在脚本中使用一个命令运行2个命令:
"scripts":{
"start":"any command",
"singleCommandToRunTwoCommand":"some command here && npm start"
}
现在转到您的终端并在那里运行npm run singleCommandToRunTwoCommand
。
例:
"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”正在构建角度应用程序,然后从目录中删除所有旧文件,最后复制结果构建文件。
假设我的“ 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_advertisements”,我将直接转到终端并输入
npm run export_advertisements
不客气😊😊
文章标签:javascript , node.js , package.json , run-script
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!