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