【问题标题】:How to use nodemon with JSX?如何在 JSX 中使用 nodemon?
【发布时间】:2015-04-08 19:59:26
【问题描述】:

我可以用一个命令编译和运行我的 JSX 应用程序:

jsx app.jsx | node

但我也希望我的服务器在每次修改app.jsx 时自动重启。我可以用nodemon 做到这一点,但我不太清楚如何让 nodemon 事先通过 JSX 编译器运行我的脚本。

我有一个像这样设置的nodemon.json 文件:

{
    "execMap": {
        "js": "node",
        "jsx": "jsx {{filename}} | node"
    },
    "ext": "js jsx",
    "ignore": [
        ".hg",
        "node_modules",
        ".idea"
    ],
    "verbose": true
}

但是当我运行nodemon 时,它告诉我:

8 Feb 21:58:48 - [nodemon] starting `jsx app.jsx | node`
8 Feb 21:58:48 - [nodemon] child pid: 10976
'\"jsx app.jsx | node\"' is not recognized as an internal or external command,
operable program or batch file.

这很奇怪,因为当我将它直接粘贴到终端时,该命令会逐字运行。

有什么方法可以让 nodemon 运行我的 JSX 文件?

【问题讨论】:

标签: node.js reactjs react-jsx nodemon


【解决方案1】:

或者您可以在 ./node_modules/.bin 目录中找到 jsx 命令并运行它:

    {
        script: "client.js",
        options: {
            execMap: {
                "js": "node",
                "jsx": "./node_modules/.bin/jsx \"$1\" | node"
            },
            ext: "js jsx",
            callback: function (nodemon) {
                nodemon.on("log", function (event) {
                    console.log(event.colour);
                });
            },
            ignore: [
                "node_modules/**/*.js",
                "public/js/**",
                "lib/api/**",
            ]
        }
    }

【讨论】:

    【解决方案2】:

    如果你在 Windows 上(像我一样),你可以创建一个 .bat 而不是像 FakeRainBrigand suggests 这样的 .sh

    @echo off
    jsx %1 | node
    

    此文件必须与nodemon.jsonpackage.json 位于同一目录中——无论出于何种原因,execMap 中的路径似乎都不起作用。


    另外,一个更简单的解决方案是在您的主/服务器脚本中使用任何 JSX,根据需要安装 node-jsx 然后 require 您的 JSX 文件。

    【讨论】:

      【解决方案3】:

      似乎 nodemon 正在尝试使用您提供的名称运行程序,而不是执行 shell。

      使用此内容创建一个 jsx.sh 文件:

      #!/bin/sh
      jsx "$1" | node
      

      然后chmod +x jsx.sh,把它放在你的nodemon.json中:

      {
          "execMap": {
              "js": "node",
              "jsx": "./jsx.sh"
          },
          "ext": "js jsx",
          "ignore": [
              ".hg",
              "node_modules",
              ".idea"
          ],
          "verbose": true
      }
      

      * 未测试

      【讨论】:

      • 我不得不改用 .bat 文件,因为我在 Windows 上,但想法是一样的。谢谢!
      猜你喜欢
      • 2021-07-10
      • 2020-05-31
      • 2018-07-04
      • 2018-07-06
      • 1970-01-01
      • 2021-06-15
      • 2019-10-15
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多