我正在尝试制作这个程序,它会自动为我设置样板。我希望它创建一些文件并向其中添加一些样板代码,安装一些基本依赖项,打开 IDE,并使用 Nodemon 运行服务器文件并在同一终端上接收所有更新的文本、数据和消息。
但是,即使可以直接在 shell 中使用nodemon,它也会抛出错误Error: spawn nodemon ENOENT
。完整错误消息:
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' ]
}
源代码:
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 spawn function is given 'node', the program runs
// However when I tried to pass '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);
});
});
当spawn函数中的第一个参数(spawn函数用于创建常量spawn)被赋予'node'时,程序运行但是当我尝试传递'Nodemon'时,我遇到错误。我尝试在网上搜索但没有找到解决方案
spawn nodemon ENOENT
通常表示系统尝试执行命令(在本例中为
nodemon
),但在系统的
PATH
变量列出的目录中找不到它。发生这种情况的原因有以下几个:
Nodemon 未全局或本地安装:确保 nodemon
已全局安装(使用
npm install -g nodemon
)或本地安装(作为使用
npm install nodemon --save-dev
的项目依赖项)。如果
nodemon
安装在本地,您通常会通过
npx nodemon
运行它。
路径问题:有时,特别是在开发环境中或在项目之间切换时,PATH
环境变量可能不包含安装
nodemon
的目录。
权限:确保您拥有执行nodemon
所需的权限。如果您全局安装它,您可能需要管理员权限(在 Unix/Linux 系统上
sudo
)。
检查安装:通过运行 nodemon
或
nodemon -v
(如果本地安装)验证是否已安装
npx nodemon -v
。如果没有安装,请安装。
检查PATH环境变量:确保安装nodemon
的目录包含在系统的
PATH
环境变量中。您可以通过分别在终端或命令提示符中运行
echo $PATH
(在 Unix/Linux 上)或
echo %PATH%
(在 Windows 上)来检查这一点。
使用 npx 运行:如果 nodemon
作为项目依赖项本地安装,请使用
npx nodemon app.js
而不是仅使用
nodemon app.js
来运行它。
权限:如果您使用的是 Unix/Linux 并全局安装了 nodemon
,请确保正确设置可执行权限。您可以使用
chmod +x /path/to/nodemon
来完成此操作。
spawn nodemon ENOENT
错误并成功运行您的 Node.js 应用程序
nodemon
。