getting error: “spawn nodemon ENOENT” even though Nodemon is globally installed and can be used directly in shell

I am trying to make this program which automatically setup a boilerplate for me. I wanted it to make some files and add some boilerplate code to it, install some basic dependencies, open the IDE and also to run the server file Using Nodemon and to recieve all the updated, texts, data, messages on the same terminal.

However, It threw the error Error: spawn nodemon ENOENT even though nodemon can be used in he shell directly.

Full Error Message:

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: spawn nodemon ENOENT
    at ChildProcess._handle.onexit (node:internal/child_process:283
:19)
    at onErrorNT (node:internal/child_process:476:16)
    at process.processTicksAndRejections (node:internal/process/tas
k_queues:82:21)
Emitted 'error' event on ChildProcess instance at:
    at ChildProcess._handle.onexit (node:internal/child_process:289
:12)
    at onErrorNT (node:internal/child_process:476:16)
    at process.processTicksAndRejections (node:internal/process/tas
k_queues:82:21) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'spawn nodemon',
  path: 'nodemon',
  spawnargs: [ 'app.js' ]
}

Source Code:

const path = require("path");
const fs = require("fs");
const { exec, spawn } = require("child_process");

const dependencies = ['express', 'ejs']

const html = `<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet/css" src="/style.css">
    <title>Document</title>
</head>
<body>
    Lorem ipsum dolor sit ammet;
</body>
</html>`

const css = 
`
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
`

const nodeJs = `
const express = require("express");
const path = require("path");
const PORT = 3000 || process.env.PORT;
const app = express();

app.set('view-engine', 'ejs');
app.set('views', path.join(__dirname, '/views'));
app.use(express.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, '/static')));

app.get('/', (req, res) => {
    res.render('index.ejs');
});

app.listen(PORT, () => {
    console.log(Application running on port ${PORT});
});
`

fs.mkdirSync(path.join(__dirname, '/myProject'));
fs.mkdirSync(path.join(__dirname, '/myProject/views'));
fs.mkdirSync(path.join(__dirname, '/myProject/static'));

fs.appendFileSync(path.join(__dirname, '/myProject/app.js'), nodeJs);
fs.appendFileSync(path.join(__dirname, '/myProject/static/style.css'), css);
fs.appendFileSync(path.join(__dirname, '/myProject/views/index.ejs'), html);


exec(yarn add ${dependencies.join(' ')}, {cwd: path.join(__dirname, '/myProject')}, (err, stdout, stderr) => {
    if (err) {
        console.log(err);
        return;
    }
    console.log(stdout);

    // When the first argument in the child variable which states the command was node, the program ran
    // However when I tried to go with nodemon, I face error
 
    const child = spawn('nodemon', ['app.js'], { cwd: path.join(__dirname, '/myProject'), stdio: 'inherit' });

    child.on('close', (code) => {
        console.log(child process exited with code ${code});
    });

    exec('code .', { cwd: path.join(__dirname, '/myProject') }, (err, stdout, stderr) => {
        if (err) console.log(err);
        console.log(stdout);
    });
});

When the first argument in the child variable which states the command was node, the program ran However when I tried to go with Nodemon, I face error.

I tried searching on the web but found no solution

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật