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 v21.6.2 Documentation
nodejs wait for exec in function
I like to integrate exec from nodejs in a custom function to handle all the errors in this one function. const exec = re...

コメント

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