2

Is there a way to configure the Node.js console window (node.exe) to run a script on startup?

I'd like to do some minor initialization automatically each time, such as setting variables and colors.

It's not sufficient to just run node.exe myinitscript.js. That will run the script and then exit.

I'd like to run the script, and then remain in in the console environment.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Making a npm module which sets colors like https://www.npmjs.com/package/color-util-logs#readme and require it is the best way i guess – suish Mar 10 '15 at 02:36

2 Answers2

4

The easiest way would probably be to create your own script that starts the repl instead. Create a script with:

global.something = 'blah';

require('repl').start({});

That will do some init and create some globals or whatever, and then run the repl, just like if you ran node.exe directly.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
0

Try setting the start or the predeploy property of the scripts object of your package.json.

The start property: The start-up script for the package. When running npm start this script will be called.

The predeploy property: The pre-deploy script for this application. This script will run before a snapshot of your package has been created. It can therefore be used to compile and optimize assets before it's upload to your application.

For your reference, here's a cheatsheet for the general syntax/structure of the package.json file.

http://browsenpm.org/package.json

shmuli
  • 5,086
  • 4
  • 32
  • 64
  • I'm just running node.exe. I'm not creating a package or running npm. Is there a general place to put an npm package that will always get executed when node.exe runs? – Matt Johnson-Pint Mar 10 '15 at 02:38