代码之家  ›  专栏  ›  技术社区  ›  mpen

如何在JSX中使用nodemon?

  •  10
  • mpen  · 技术社区  · 10 年前

    我可以用一个命令编译和运行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文件?

    3 回复  |  直到 9 年前
        1
  •  6
  •   Brigand    10 年前

    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
    }
    

    *未经测试

        2
  •  2
  •   marksyzm    10 年前

    或者,您可以在 ./node_modules/.bin 目录,然后运行它:

        {
            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/**",
                ]
            }
        }
    
        3
  •  1
  •   Community CDub    8 年前

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

    @echo off
    jsx %1 | node
    

    此文件必须与位于同一目录中 nodemon.json package.json --路径似乎在 execMap 无论出于什么原因。


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