Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 959
mpen Avatar asked Feb 09 '15 06:02

mpen


3 Answers

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

like image 161
Brigand Avatar answered Oct 18 '22 01:10

Brigand


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/**",
            ]
        }
    }
like image 2
marksyzm Avatar answered Oct 18 '22 02:10

marksyzm


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.

like image 1
mpen Avatar answered Oct 18 '22 02:10

mpen