10

I can compile and run my JSX app with one command:

jsx app.jsx | node

But I also want my server to automatically restart every time I modify app.jsx. I can do that with nodemon, but I can't quite figure out how to get nodemon to run my script through the JSX compiler beforehand.

I've got a nodemon.json file set up like this:

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

But when I run nodemon it tells me:

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.

Which is odd, because that command works verbatim when I paste it directly into my terminal.

Is there any way I get nodemon to run my JSX files?

4

3 回答 3

6

似乎 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
}

* 未经测试

于 2015-02-09T16:55:10.213 回答
2

或者您可以在您的./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/**",
            ]
        }
    }
于 2015-04-05T19:18:32.320 回答
1

如果您在 Windows 上(像我一样),您可以创建一个.bat而不是.shFakeRainBrigand建议的

@echo off
jsx %1 | node

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


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

于 2015-02-09T19:43:12.570 回答