Hubot windows service - node.js

I would like to wrap my Hubot in a windows service to deploy it.
I am using node-windows to do this but I'm having some trouble trying to get it to run a coffee-script file (which is what hubot uses).
If i manually run
> coffee .\node_modules\hubot\bin\hubot
everything works fine.
But I cant work out how to call this from a node-windows script. My attempt below:
var Service = require('node-windows').Service;
var svc = new Service({
name:'Hubot',
description: 'Hubot',
script: 'coffee .\\hubot\\node_modules\\hubot\\bin\\hubot'
});
svc.on('install',function(){
svc.start();
});
svc.install();
which fails with the following:
C:\Users\luke.mcgregor\hubot>node app.js fs.js:747 return
binding.mkdir(pathModule._makeLong(path),
^ Error: ENOENT, no such file or directory 'C:\Users\luke.mcgregor\hubot\coffee
.\hubot\node_modules\hubot\bin\daemon'
at Error (native)
at Object.fs.mkdirSync (fs.js:747:18)
at C:\Users\luke.mcgregor\AppData\Roaming\npm\node_modules\node-windows\lib\daemon.js:409:16
at FSReqWrap.cb [as oncomplete] (fs.js:226:19)

The script is not a command, only the path to a file. You can change the executable used by node-windows to run your scripts by setting an execPath environment variable: https://github.com/coreybutler/node-windows/issues/61#issuecomment-51423542

Related

Using nodejs app as Windows Service causes EPERM-4048 ERROR

I'm using "node-windows" for creating service that will run my app.js server
on windows.
This is the code that makes the service (using node-windows module)-
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'Demo Landing Page',
description: 'The nodejs.org example web server.',
script: 'app.js'
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
svc.install();
All works fine with the script but this line for reading directory content-
fs.readdir([PATH], function(err, builds)
This is the error that I get -
{Error: EPERM: operation not permitted, scandir [PATH]
errno: -4048,
code: 'EPERM',
syscall: 'scandir',
path: 'app.js' }
But If I run not as a service - just as-
node app.js
all works fine, permissions are fine.
What can I do in order to fix this?
Or maybe there is another option for running nodejs on windows server?
Thanks!
** I sign in as administrator and I have access to the following dir
Right click on service
open its properties
check what account its using under logOn
Either try this in service above code
svc.logOnAs.domain = 'mydomain.local';
svc.logOnAs.account = 'username';
svc.logOnAs.password = 'password';
or you can directly add credentials and restart the service

readFileSync throws error when server launched as linux service

i'm trying to make a simple api for myself using a node/express server running on digital ocean. in the server file i have something like this:
var data = fs.readFileSync('path/to/data.json','utf8');
which works perfectly fine when i launch the server manually from the cmd line
node server
but what i have setup is a linux service so that everytime i restart my digital ocean machine it will automatically launch the server, the service ( kept in etc/init/ ) looks like this:
start on filesystem and started networking
respawn
exec node /path/to/server.js
the issue is that when I make the request to the server that runs the readFileSync call it works fine if the server had been launched manually from the cmd line, but when the server was launched via the service then the readFileSync throws the following error:
Error: ENOENT, no such file or directory 'path/to/data.json'
at Error (native)
at Object.fs.openSync (fs.js:500:18)
at Object.fs.readFileSync (fs.js:352:15)
the file and the directory do exist ( if i make a request for the data.json file directly in my browser i can see it )
what am i missing? is there something about launching the server as a service that conflics with using readFileSync? is there an alternative approach to what i'm trying to do? should i use some kind of request/fetch resource module for accessing that json file?
You're using a relative path but the process is not being started from where you think it is. Instead of using relative paths, use absolute paths.
So if your layout looks like:
server.js
path/
to/
data.json
Then inside your server.js, you can just do something like:
var path = require('path');
// ...
var data = fs.readFileSync(path.join(__dirname, 'path/to/data.json'), 'utf8');

First time gulp user, can't build skeleton project. I don't see anything else on the web with this error

Hi this is my first time using gulp, and I'm trying to use typescript/gulp/express to form a RESTful web service.
I found a skeleton project for this but I get an error when I try to build it.
Here's a link to the project:
https://github.com/idanov/express-ts-rest
Kevins-Mac-Pro:express-ts-rest kevincmanning$ gulp buildAll
{ file: undefined,
start: undefined,
length: undefined,
messageText: 'Failed to parse file \'src/tsconfig.json\': text.charCodeAt is not a function.',
category: 1,
code: 5014 }
{ file: undefined,
start: undefined,
length: undefined,
messageText: 'Failed to parse file \'tests/tsconfig.json\': text.charCodeAt is not a function.',
category: 1,
code: 5014 }
[00:59:23] Using gulpfile ~/Projects/express-ts-rest/gulpfile.js
[00:59:23] Starting 'build'...
[00:59:23] 'build' errored after 10 ms
[00:59:23] TypeError: Cannot read property 'on' of null
at DestroyableTransform.Readable.pipe (/Users/kevincmanning/Projects/express-ts-rest/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:516:7)
at Gulp.<anonymous> (/Users/kevincmanning/Projects/express-ts-rest/gulpfile.js:35:10)
at module.exports (/Users/kevincmanning/Projects/express-ts-rest/node_modules/orchestrator/lib/runTask.js:34:7)
at Gulp.Orchestrator._runTask (/Users/kevincmanning/Projects/express-ts-rest/node_modules/orchestrator/index.js:273:3)
at Gulp.Orchestrator._runStep (/Users/kevincmanning/Projects/express-ts-rest/node_modules/orchestrator/index.js:214:10)
at Gulp.Orchestrator.start (/Users/kevincmanning/Projects/express-ts-rest/node_modules/orchestrator/index.js:134:8)
at /usr/local/lib/node_modules/gulp-cli/lib/versioned/^3.7.0/index.js:46:20
at nextTickCallbackWith0Args (node.js:415:9)
at process._tickCallback (node.js:344:13)
at Function.Module.runMain (module.js:443:11)
The project runs fine under Ubuntu. However, there is an issue with the gulp-tsb package for OS X which causes your problem. You can follow up the progress at https://github.com/jrieken/gulp-tsb/issues/34
Meanwhile if you want to hack around it, just edit node_modules/gulp-tsb/lib/index.js on line 11 by replacing fs_1.readFileSync(path, undefined); with fs_1.readFileSync(path, 'utf-8');.
Steps to Run the tasks:
First download the project
Navigate to the project folder in terminal and do npm install
After successfull installation do npm install -g gulp This installs gulp globally.
Run a gulp task. For example at terminal on the project folder, gulp build executes the gulp task called build.

ENOENT on writeFileSync works on repl fails on gulp-asset-manifest

I'm using the code on this site to write a gulp file for a Windows aspnet + angularjs app.
I'm running only styles for now, and when I run gulp I get:
Error: ENOENT, no such file or directory 'C:\project\build\manifest-debug.json'
at Error (native)
at Object.fs.openSync (fs.js:502:18)
at Object.fs.writeFileSync (fs.js:1103:15)
at writeManifestFile (C:\project\node_modules\gulp-asset-manifest\index.js:30:8)
at resetManifestFile (C:\project\node_modules\gulp-asset-manifest\index.js:51:5)
at module.exports (C:\project\node_modules\gulp-asset-manifest\index.js:76:5)
at getManifest (C:\project\Gulpfile.js:97:12)
at Gulp.<anonymous> (C:\project\Gulpfile.js:126:15)
at module.exports (C:\project\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:34:7)
at Gulp.Orchestrator._runTask (C:\project\node_modules\gulp\node_modules\orchestrator\index.js:273:3)
Looking at C:\project\node_modules\gulp-asset-manifest\index.js:30:8 I see:
fs.writeFileSync(filename, JSON.stringify(data));
So I ran this on node's REPL:
fs.writeFileSync('C:\\project\\build\\manifest-debug.json', 'hi');
And the file was created correctly.
I tried running gulp again with an existing file and got the exact same error.
I added a console.log(filename); before line 30 on index.js and I got:
./build/manifest-debug.json
So I ran fs.writeFileSync('./build/manifest-debug.json', 'hi'); on REPL from the same folder and it worked too.
Not sure what's going on.
I'm running everything with UAC disabled and on a command prompt with admin rights.
The problem was fs.writeFileSync requires the path to exist.
At the point gulp-asset-manifest was called ./build was nonexistent, because the clean task deleted the entire path.

Issue on Deploying Node.js & Socket.IO App to Nodejitsu

I am new at Node.js, Socket.IO, and Nodejitsu. I am also running Ubuntu on my machine. After Googling for free hoisting supports for Node.js and Socket.IO, I find the Nodejitsu hosting platform. They say it is easy to deploy an app but I find it a little complicated. Here is the Process which I followed.
After setting up my account with Nodejitsu I ran:
sudo npm install jitsu -g
sudo jitsu install socket.io
And I tried to deploy an app by using:
jitsu deploy
but I got two warnings on cmd line as:
warn: There is no package.json file in /home/ben
warn: Creating package.json at /home/ben/package.json
and the the jitsu is prompting as:
prompt: Application name: (ben)
prompt: Subdomain name: (cand-ben)
prompt: scripts.start:
First off, I don't understand what those two warnings are for? Should I create a package.json file there? I also tried to start a script like this in command line after prompt: scripts.start:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('hello, i know nodejitsu\n');
}).listen(8080);
but I am getting this error messages
prompt: scripts.start: var http = require('http');
error: Invalid input for scripts.start
error: Start script was not found in /home/ben
prompt: scripts.start: http.createServer(function (req, res) {
error: Invalid input for scripts.start
error: Start script was not found in /home/ben
prompt: scripts.start: res.writeHead(200, {'Content-Type': 'text/plain'});
error: Invalid input for scripts.start
error: Start script was not found in /home/ben
prompt: scripts.start: res.end('hello, i know nodejitsu\n');
error: Invalid input for scripts.start
error: Start script was not found in /home/ben
prompt: scripts.start: }).listen(8080);
Can you please let me know how should I pass the script into the command prompt? How should I create the package.json? is there a any clear tutorial illustrating these thing?
Thanks
Error After adding the package.json from Peter site.
warn: About to write /home/ben/package.json
data:
data: {
data: engines: { node: '0.8.x' },
data: name: 'ben',
data: scripts: { start: '' },
data: subdomain: 'candente-ben',
data: version: '0.0.0'
data: }
data:
prompt: Is this ok?: (yes)
info: Analyzing application dependencies in
WARN package.json ben#0.0.0 No repository field.
WARN package.json ben#0.0.0 No readme data.
info: Checking app availability ben
info: Checking app availability ben
info: Creating app ben
info: Creating app ben
error: Error creating ben
error: Nodejitsu Error (500): Internal Server Error
error: Error running command deploy
error: Nodejitsu Error (500): Internal Server Error
warn: Error returned from Nodejitsu
error: Error: App name / subdomain combination is not available
error: at module.exports (/root/nodejitsu/lib/nodejitsu/resources/app/controller/available.js:27:17)
error: at Resource._request (/root/nodejitsu/node_modules/resourceful/lib/resourceful/resource.js:184:13)
error: at Function.Resource.runAfterHooks (/root/nodejitsu/node_modules/resourceful/lib/resourceful/resource.js:93:12)
error: at Resource._request (/root/nodejitsu/node_modules/resourceful/lib/resourceful/resource.js:179:14)
error: at Couchdb.view (/root/nodejitsu/node_modules/resourceful/lib/resourceful/engines/couchdb/index.js:143:5)
error: at Request._onResponse [as _callback] (/root/nodejitsu/node_modules/resourceful/node_modules/cradle/lib/cradle.js:233:9)
error: at Request.init.self.callback (/root/nodejitsu/node_modules/request/main.js:120:22)
error: at Request.EventEmitter.emit (events.js:99:17)
error: at Request.<anonymous> (/root/nodejitsu/node_modules/request/main.js:555:16)
error: at Request.EventEmitter.emit (events.js:96:17)
help: For help with this error contact Nodejitsu Support:
help: webchat: <http://webchat.nodejitsu.com/>
help: irc: <irc://chat.freenode.net/#nodejitsu>
help: email: <support#nodejitsu.com>
help:
help: Copy and paste this output to a gist (http://gist.github.com/)
info: Nodejitsu not ok
First of I didn't understand what those two warnings are for? Should I create a package.json file there?
Yes. A package.json file is required for nodejitsu deployment to work. It describes the environment your application needs in order to operate.
I also tried to start a script like this in command line after prompt: scripts.start:
The scripts.start cannot be javascript code, it must be a simple shell command. Take that javascript code and save it in a file called server.js and then make your scripts.start command be node server.js.
Here's Nodejitsu's interactive guide to the package.json file. Of course the official npm docs on package.json are a must-read. And here is an open-source app I wrote that is deployed on nodejitsu if you want to reference that. The basic scripts.start property will need to be along the lines of:
"scripts": {"start": "node server.js"}
Update
So you are probably trying too many things at once here, without spending enough time on the basics to stay out of trouble. Do not skip any of these steps. You will learn and comprehend in the process and you'll be able to understand future errors on your own.
First, get your app running locally with a manual command like node server.js.
Second, the the command npm start working locally by getting your package.json correct.
Third, then work on making it work on nodejitsu.

Resources