问题遇到的现象和发生背景
node自定义命令行工具cli,运行报错Error: spawn npm ENOENT
问题相关代码,请勿粘贴截图
const {promisify} = require ('util')
const figlet = promisify(require('figlet'))
const clear = require('clear')
const chalk = require('chalk')
const log = content => console.log(chalk.green(content))
const {clone} = require('./download')
// proc 是子进程本身
//stdout 是 子进程的输出流
const spawn = async (...args) =>{
const {spawn} = require('child_process')
return new Promise(resolve =>{
const proc = spawn(...args)
proc.stdout.pipe(process.stdout)
proc.stderr.pipe(process.stderr)
proc.on('close',()=>{
resolve()
})
})
}
module.exports = async name =>{
// 打印欢迎界面
clear()
const data = await figlet(`${name} Welcome`)
log(data)
// 克隆项目
await clone('github:su37josephxia/vue-template',name)
// 安装依赖
log('安装依赖')
await spawn('npm',['install'],{cwd: `./${name}` })
log('安装成功')
}
运行结果及报错内容
√ 下载 github:su37josephxia/vue-template
安装依赖
node:events:505
throw er; // Unhandled 'error' event
^
Error: spawn npm ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
at onErrorNT (node:internal/child_process:478:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)
at onErrorNT (node:internal/child_process:478:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -4058,
code: 'ENOENT',
syscall: 'spawn npm',
path: 'npm',
spawnargs: [ 'install' ]
}
PS E:\node-proj\vue-auto-router-cli2>
我的解答思路和尝试过的方法
查了很多文章,都说是因为没有给npm配置环境变量。然而我把npm和cmd的环境变量都认真检查过了,都是配置好的。并没有卵用。
我想要达到的结果
有没有其他思路可以解决此问题的?