Search code examples
node.jsreactjsnodemon

How to use nodemon with JSX?


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?


Solution

  • It seems nodemon is attempting to run a program with the name you provide, rather than executing a shell.

    Create a jsx.sh file with this content:

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

    Then chmod +x jsx.sh, and put this in your nodemon.json:

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

    * not tested