简体   繁体   English

如何在JSX中使用nodemon?

[英]How to use nodemon with JSX?

I can compile and run my JSX app with one command: 我可以用一个命令编译和运行我的JSX应用程序:

jsx app.jsx | node

But I also want my server to automatically restart every time I modify app.jsx . 但我也希望每次修改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. 我可以用nodemon做到这一点 ,但我无法弄清楚如何让nodemon事先通过JSX编译器运行我的脚本。

I've got a nodemon.json file set up like this: 我有一个nodemon.json文件设置如下:

{
    "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: 但是当我运行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.

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? 有没有办法让nodemon运行我的JSX文件?

It seems nodemon is attempting to run a program with the name you provide, rather than executing a shell. 似乎nodemon正在尝试使用您提供的名称运行程序,而不是执行shell。

Create a jsx.sh file with this content: 使用此内容创建一个jsx.sh文件:

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

Then chmod +x jsx.sh , and put this in your nodemon.json: 然后chmod +x jsx.sh ,并把它放在你的nodemon.json中:

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

* not tested *未经测试

OR you can just locate the jsx command in your ./node_modules/.bin directory and run it off that instead: 或者您可以在./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/**",
            ]
        }
    }

If you're on Windows (like me) you can create a .bat instead of a .sh like FakeRainBrigand suggests 如果你在Windows上(像我一样)你可以创建一个.bat而不是.shFakeRainBrig并建议

@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. 此文件必须与nodemon.jsonpackage.json位于同一目录中 - 无论出于何种原因,路径似乎在execMap中都execMap


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. 此外,更简单的解决方案是在主/服务器脚本中使用任何JSX,安装node-jsx ,然后根据require JSX文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM