V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cs5117155
V2EX  ›  Node.js

终端命令下,什么命令 node 可以同时运行多个*.js 文件

  •  
  •   cs5117155 · 2022-01-21 10:41:08 +08:00 · 8233 次点击
    这是一个创建于 797 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如我文件夹下面有`a.js `,`b.js` ,`c.js`等等。每次运行都要 `node a.js & b.js & c.js`.
    有什么方法可以一次性执行当前文件夹下的所有 js 文件吗
    13 条回复    2022-02-28 15:33:59 +08:00
    alphanow
        1
    alphanow  
       2022-01-21 11:35:24 +08:00
    写个 index.js ,用 fs.readdir 把文件都读出来,然后再 map -> execFile
    以后 node .就行了
    momocraft
        2
    momocraft  
       2022-01-21 11:38:15 +08:00
    xargs
    shyling
        3
    shyling  
       2022-01-21 11:39:25 +08:00
    ls *.js | xargs -n 1 node
    b1eberg0n
        4
    b1eberg0n  
       2022-01-21 12:06:27 +08:00
    concurrently
    ryncv
        5
    ryncv  
       2022-01-21 12:09:56 +08:00
    ./start.sh (狗头
    ysc3839
        6
    ysc3839  
       2022-01-21 12:17:10 +08:00
    find -name '*.js' -exec node {} \;
    akira
        7
    akira  
       2022-01-21 13:38:10 +08:00
    手动写个脚本,需要增加或者删除的时候 手动处理。 生产环境不要偷懒
    xylxAdai
        8
    xylxAdai  
       2022-01-21 13:39:42 +08:00
    shell 脚本
    cs5117155
        9
    cs5117155  
    OP
       2022-01-21 14:12:17 +08:00
    @shyling 这个最方便,厉害
    jaredyam
        10
    jaredyam  
       2022-01-22 15:52:18 +08:00
    我觉得#6 语义上更符合直觉,如果模式比较固定,放 Makefile 里岂不更好?
    wangtian2020
        11
    wangtian2020  
       2022-01-28 13:18:00 +08:00
    用 nodejs 的 fs 模块读取文件夹下面的文件,生成 powershell 脚本,用 child_process 模块运行脚本
    能用 nodejs 写都能用 nodejs 写
    SoulGem
        12
    SoulGem  
       2022-02-17 16:26:41 +08:00
    pm2
    写个入口文件,遍历*.js 然后 pm2.start()
    dany813
        13
    dany813  
       2022-02-28 15:33:59 +08:00
    写个 node 入口脚本
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3322 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:30 · PVG 21:30 · LAX 06:30 · JFK 09:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.