0

I try to run one nodejs application inside of docker container. Everything is as expect once I get into docker container and execute the application. However, once I use docker exec to send the command to container. The error is occured as TypeError: Object # has no method 'execSync'. How can I use child_prcess in my node application from docker container?

root@17dbb03697b0:/apps# cat test.js
var aaa = require('child_process').execSync('echo "aaaaaaaaa"').toString().trim();
console.log(aaa);
root@17dbb03697b0:/apps# node test.js
aaaaaaaaa
root@17dbb03697b0:/apps#

user@ip-172-1-1-121:~/$ docker exec containername bash -c "cd /apps;node test.js"

/apps/test.js:1
module, __filename, __dirname) { var aaa = require('child_process').execSync('
                                                                    ^
TypeError: Object #<Object> has no method 'execSync'
    at Object.<anonymous> (/apps/test.js:1:98)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
user@ip-172-1-1-121:~/$ docker exec containername bash -c "cd /apps/;cat test.js"
var aaa = require('child_process').execSync('echo "aaaaaaaaa"').toString().trim();
console.log(aaa);
jacobcan118
  • 7,797
  • 12
  • 50
  • 95
  • I just tested this with `docker run --rm -ti node:latest node` and `require('child_process')` explicitly shows that `execSync` is there. – Yan Foto Aug 19 '16 at 22:24

1 Answers1

0

execSync() was introduced in the v0.11.12 release.

Check your node version.

docker exec containername bash -c "node -v"
Nehal J Wani
  • 16,071
  • 3
  • 64
  • 89
  • it turns out i use nvm install node v4.2.3, after checking, /usr/bin/node is old version. what I did is making a soft link and it is fine now. user@ip-172.0.0.1:$ docker exec copilot bash -c "/usr/bin/nodejs -v" v0.10.25 user@ip-172.0.0.1:$ docker exec copilot bash -c "which node" /usr/bin/node user@ip-172.0.0.1:$ docker exec copilot bash -c "node -v" v4.2.3 – jacobcan118 Aug 24 '16 at 15:36