如何将自定义脚本添加到运行 javascript 文件的 package.json 文件中?

问题描述 投票:0回答:8

我希望能够在将运行

script1
的项目目录中执行命令
node script1.js

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

到目前为止我已经尝试添加:

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

到我的 package.json 文件,但是当我尝试运行

script1
时,我得到以下输出:

zsh: command not found: script1

有人知道将上述脚本添加到项目文件夹所需的步骤吗?

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

如果您需要任何说明,请告诉我。

javascript node.js package.json run-script
8个回答
374
投票

自定义脚本

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


49
投票

我创建了以下内容,它正在我的系统上运行。请尝试这个:

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" 
  }

40
投票

步骤如下:

  1. 在package.json中添加:

    "bin":{
        "script1": "bin/script1.js" 
    }
    
  2. 在项目目录下创建

    bin
    文件夹并添加文件
    runScript1.js
    ,代码为:

    #! /usr/bin/env node
    var shell = require("shelljs");
    shell.exec("node step1script.js");
    
  3. 在终端中运行

    npm install shelljs

  4. 在终端中运行

    npm link

  5. 现在可以从终端运行

    script1
    它将运行
    node script1.js

参考:http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm


12
投票

假设在脚本中您想用一个命令运行 2 个命令:

"scripts":{
  "start":"any command",
  "singleCommandToRunTwoCommand":"some command here && npm start"
}

现在转到您的终端并运行

npm run singleCommandToRunTwoCommand


10
投票

假设我的“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

3
投票

就我而言,我太愚蠢了,我运行了以下命令

node run build

而不是下面的命令

npm run build

在清理并重新运行安装之前,请重新检查您的命令。

另外请不要忘记npx,您可以使用任何模块而无需安装它。


1
投票

示例:

  "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”正在构建角度应用程序,然后从目录中删除所有旧文件,最后复制结果构建文件。


0
投票

截至 2024 年更新

今年发布的 Node.js 22 LTS 附带了

npm run
的更新的原生替代品。

要执行自定义脚本,请使用

node --run
:

node --run <custom_script_name> 

因此,您可以使用

package.json
脚本获得像这样的
start
文件:

{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  }
}

假设您的

index.js
文件包含以下内容:

console.log('Program runs')

在终端中,您可以使用

start
命令运行
package.json
中的
node --run
脚本,如下所示:

node --run start

输出将如下所示:

Program runs
(node:22407) ExperimentalWarning: Task runner is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

因为它是实验性的,你会收到警告。

© www.soinside.com 2019 - 2024. All rights reserved.