【问题标题】:Using npm commands to start NodeJS project with arguments使用 npm 命令启动带参数的 NodeJS 项目
【发布时间】:2014-02-16 07:06:44
【问题描述】:

我正在尝试创建两个单独的 npm 命令来在开发机器上本地启动我的 NodeJS 项目并在生产模式下启动。我希望能够分别将参数传递给机器以提供正确的依赖项 - 这可能是生产中的 CDN 或我的本地机器。

这是我希望在 package.json 中包含的内容

"run": "node ./server/app.js", /* for running locally*/

"start": "node ./server/app.js", /* for running in production*/

如果我尝试调用 npm run - 它会创建此错误:

npm ERR! npm run-script [<pkg>] <command>
npm ERR! not ok code 0

我还希望能够发送包含 URL 的命令行参数。

【问题讨论】:

    标签: node.js npm


    【解决方案1】:

    你做得不对。

    要调用自定义脚本,您必须运行

    npm run-script run
    

    你的 package.json 应该有:

    "scripts": {
        "start": "node ./server/app.js",
        "run": "node ./server/app.js"
    }
    

    见:https://npmjs.org/doc/cli/npm-run-script.html

    【讨论】:

      【解决方案2】:

      您可以使用此 bash 脚本直接使用参数运行任何(甚至是嵌套的)模块。不要忘记将~/Develop/node_modules 更改为您的本地路径。

      #!/bin/bash
      if [ $# -eq 0 ]; then
          echo Usage: npmrun module [args]
          exit
      fi
      data=~/bin/npmrun.data
      if [ -f $data ]; then source $data
      else declare -A where
      fi
      cd ~/Develop/node_modules
      if [ -z ${where[$1]} ]; then
          path=$(find -path '*/bin/'$1)
          if [ -z $path ]; then
              echo "Module \"$1\" not founded!"
              exit
          else
              where[$1]=$path;
              declare -p where > $data
          fi
      fi
      ${where[$1]} ${@:2}
      

      【讨论】:

        猜你喜欢
        • 2018-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-19
        • 2017-04-16
        • 2021-02-21
        • 1970-01-01
        • 2022-01-11
        相关资源
        最近更新 更多