Last Updated: February 25, 2016
·
460
· mike hedman

Run configuration matters in PhpStorm

Hi,
Today I learned a painful lesson about running LoopBack from inside of PhpStorm (or probably any IDE). The run configuration asks for a working dir, and a script. I had:

Working Directory: /Users/mike/dev/lb/server
JavaScript File: server.js

And everything appeared to work for me. But it encouraged me to write a bug :(

When I added Bunyan to my server.js file for logging, I added:

if (config.isDev) {
  var spawn = require('child_process').spawn;
  var bunyanCLI = spawn('../node_modules/bunyan/bin/bunyan', ['--color'], { stdio: ['pipe', process.stdout]     
});
  logOptions.stream = bunyanCLI.stdin;
}

The error was the leading ".." in the path. I looked in my file history, and see that I had originally (correctly!) used just '.', but it didn't work when I ran it through PhpStorm, so I then changed it to '..' - which was bad. When I then went to the console and tried to run the app by typing "slc run" from the root directory, I got the following (not terribly helpful) error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: write EPIPE
    at errnoException (net.js:904:11)
    at Object.afterWrite (net.js:720:19)

So when the current dir was the root, the dot-dot was making it so that the spawn command could not find the bunyan executable. This was masked by always running from the /server directory when using PhpStorm.

The code resolution was to not use a hard coded path:

var bunyanCLI = spawn(path.resolve(__dirname, '../node_modules/bunyan/bin/bunyan'), ['--color'], { stdio: ['pipe', process.stdout] });

But the original "bug" was in my Run Configuration that pushed me into writing the bug in the code. So I've changed the run config to always run from the root dir:

Working Directory: /Users/mike/dev/lb/
JavaScript File: server/server.js

I didn't know you could include a directory path in the JavaScript File item, but you can...and you should.