在mac中,我从bash切换到zsh,然后zsh切换回bash。
chsh -s zsh
切换回bash
'use strict'
const sh = require('shell-exec')
module.exports = function (port, method = 'tcp') {
port = Number.parseInt(port)
if (!port) {
return Promise.reject(new Error('Invalid argument provided for port'))
}
if (process.platform === 'win32') {
return sh(
`Stop-Process -Id (Get-Net${method === 'UDP' ? 'UDP' : 'TCP'}Connection -LocalPort ${port}).OwningProcess -Force`
)
}
return sh(
`lsof -i ${method === 'udp' ? 'udp' : 'tcp'}:${port} | grep ${method === 'udp' ? 'UDP' : 'LISTEN'} | awk '{print $2}' | xargs kill -9`
)
}
TypeError: sh is not a function更新1:
https://github.com/tiaanduplessis/shell-exec/blob/master/src/index.ts
import * as childProcess from "child_process";
type Command = string | string[];
interface CmdResult {
code?: number | null;
stdout: string;
stderr: string;
error?: Error;
cmd: string;
}
export interface FailedExec extends CmdResult {
error: Error;
}
export interface SuccessfulExec extends CmdResult {
code: number | null;
}
function shellExec(
cmd: Command,
opts?: Omit<childProcess.SpawnOptionsWithoutStdio, "stdio" | "cwd">
): Promise<CmdResult> {
const executable = Array.isArray(cmd) ? cmd.join(";") : cmd;
const options: childProcess.SpawnOptionsWithoutStdio = {
...opts,
stdio: "pipe",
cwd: process.cwd(),
};
const { platform } = process;
try {
const cmd = platform === "win32" ? "cmd" : "sh";
const arg = platform === "win32" ? "/C" : "-c";
const child = childProcess.spawn(cmd, [arg, executable], options);
return new Promise((resolve) => {
const stdoutList: string[] = [];
const stderrList: string[] = [];
if (child.stdout) {
child.stdout.on("data", (data) => {
if (Buffer.isBuffer(data)) return stdoutList.push(data.toString());
stdoutList.push(data);
});
}
if (child.stderr) {
child.stderr.on("data", (data) => {
if (Buffer.isBuffer(data)) return stderrList.push(data.toString());
stderrList.push(JSON.stringify(data));
});
}
const getDefaultResult = () => {
const stderr = stderrList.join("\n");
const stdout = stdoutList.join("\n");
return { stdout, stderr, cmd: executable };
};
child.on("error", (error) => resolve({ ...getDefaultResult(), error }));
child.on("close", (code) => resolve({ ...getDefaultResult(), code }));
});
} catch (error) {
return Promise.reject(error);
}
}
export default shellExec;在我的react项目中,shell-exec看起来不存在吗?
发布于 2022-05-31 13:21:29
你的代码应该能用。作为一种解决办法,尝试:
const sh = require('shell-exec').defaulthttps://stackoverflow.com/questions/72441147
复制相似问题