child_processをESMで記述する

child_processをESMで記述する

node:child_processをESMで記述します。

index.mjs

import fs from 'fs/promises'
import {exec} from 'node:child_process'
import util from 'node:util'
await fs.writeFile('/tmp/test.txt', 'comming soon.')
const {stdout,stderr} = await util.promisify(exec)('ls -la /tmp')
console.log(stdout)

実行します。

$ node index.mjs

/tmp配下のlsコマンド結果が出力されます。

参考サイト

Child process | Node.js v19.7.0 Documentation
nodejs wait for exec in function
Iliketointegrateexecfromnodejsinacustomfunctiontohandlealltheerrorsinthisonefunction.constexec=require('child_process').exec;functionos_func(){this.execCommand=...

コメント

タイトルとURLをコピーしました