【问题标题】:How do I add a custom script to my package.json file that runs a javascript file?如何将自定义脚本添加到运行 javascript 文件的 package.json 文件中?
【发布时间】:2016-07-25 19:30:59
【问题描述】:

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

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

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

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

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

zsh: command not found: script1

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

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

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

【问题讨论】:

  • 你想如何运行它?你在使用“npm run script1”吗?
  • 您是否使用npm run script1 运行了script1?
  • @Claudiordgz 是对的,或者在 Sujeet 的回答中,“npm start”和“npm test”是名为“start”和“test”的脚本的快捷方式
  • 在我的终端中,我需要能够键入名为script1 的单字命令,该命令应该运行node script1.js

标签: javascript node.js package.json run-script


【解决方案1】:

你的脚本

"scripts": {
    "start": "node script1.js",
    "script2": "node script2.js"
}

命令

npm start
npm run script2

【讨论】:

    【解决方案2】:

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

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

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

      现在转到您的终端并在那里运行npm run singleCommandToRunTwoCommand

      【讨论】:

      • 虽然有帮助,但这并不能直接回答 OP 的问题。
      • 基本上我们的目的是引导您走上正确的道路,而不是您自己做。这是正确的学习方式。
      • @AdamJB 确实如此,因为他使用了 npm run
      • @kubadev,我并不是要迂腐,只是指出 OP 要求脚本根据他自己对他的问题的评论使用一个单词命令运行:In my terminal I need to be able to type the one word command called script1 which should run node script1.js
      【解决方案4】:

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

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

      【讨论】:

      • 运行script1.js文件的命令需要是自定义的单字命令script1
      • 然后把"start"改成script1,你可以用任何你喜欢的名字,我更喜欢用start来明确定义应该运行什么
      • @sujeet-jaiswal 简单地从 start 更改为 script1 是行不通的。 “start”这个词在 npm 中是保留的,所以它可以工作。单词 script1 不是,它不会被识别,即使它是在 package.json 中定义的。上面的 wesleysmyth 答案是正确的,只需在调用中添加 run。
      【解决方案5】:

      例子:

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

      【讨论】:

      【解决方案6】:

      自定义脚本

      npm run-script <custom_script_name>

      npm run <custom_script_name>

      在您的示例中,您可能希望运行 npm run-script script1npm run script1

      https://docs.npmjs.com/cli/run-script

      生命周期脚本

      Node 还允许您为某些生命周期事件运行自定义脚本,例如在运行 npm install 之后。这些可以找到here

      例如:

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

      这将在 npm install 命令之后运行 electron-rebuild

      【讨论】:

      • npm run-script scriptname 为我工作,但 npm run scriptname 没有!
      • 如何在没有“运行”命令的情况下运行自定义脚本? Sails.js 剂量 - 它有一个命令 sails lift 甚至需要 NPM。通过 NPM 安装它是否会在安装它的系统上添加一个终端脚本?如果没有,这是怎么做的?
      • @GalGrünfeld 您是否在全球范围内安装了sails(npm install -g)?
      • 我有,我做了一些阅读,并且据我所知,从 Sail 的网站上,全球安装它(通过 -g),发现 Sails 安装了 bash/cmdlet 脚本(例如 @987654335 @(bash/cmdlet 根据安装它的机器)并为机器上的这些脚本添加全局路径。
      • 如何通过我的package.json运行下载的npm包的js文件?
      【解决方案7】:

      步骤如下:

      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

      【讨论】:

      • 这应该是基于@Jake.JS 对他的问题的评论所接受的答案,即如何使用单个命令运行它。
      • 这应该是根据问题接受的答案
      猜你喜欢
      • 2022-12-03
      • 2020-04-09
      • 2018-09-08
      • 2021-08-15
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多