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?

Dmitry Shvedov
  • 3,169
  • 4
  • 39
  • 51
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • Does this work for you? https://github.com/remy/nodemon#specifying-extension-watch-list – Henrik Andersson Feb 09 '15 at 06:34
  • @limelights No. That just tells nodemon which extensions to watch, not how to handle them. I believe it has built-in support for coffeescript, but not jsx. – mpen Feb 09 '15 at 07:45

3 Answers3

6

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

Brigand
  • 84,529
  • 20
  • 165
  • 173
  • I had to use a `.bat` file instead because I'm on Windows, but the idea is the same. Thanks! – mpen Feb 09 '15 at 19:43
2

OR you can just locate the jsx command in your ./node_modules/.bin directory and run it off that instead:

    {
        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/**",
            ]
        }
    }
marksyzm
  • 5,281
  • 2
  • 29
  • 27
1

If you're on Windows (like me) you can create a .bat instead of a .sh like FakeRainBrigand suggests

@echo off
jsx %1 | node

This file has to be in the same directory as nodemon.json and package.json -- paths don't seem to work in the execMap for whatever reason.


Also, an even easier solution is to just not use any JSX in your main/server script, install node-jsx and then require your JSX files as needed.

Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236