Reputation: 546
I'm trying to install a custom module that I've just written using npm. I'd like to be able to run it like any other command on my computer.
Its called "nawk"
package.json
{
"name": "nawk",
"preferGlobal": true,
"version": "0.0.1",
"author": "My Name <[email protected]>",
"description": "a simpler version of awk",
"bin": {
"nawk": "./index.js"
},
"scripts": {
"start": "node index"
},
"dependencies" : {
},
"license": "MIT",
"engines": {
"node": ">=0.6"
}
}
index.js
require('fs');
var readline = require('readline');
var args = process.argv;
args.shift();
args.shift();
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function (line) {
var message = '';
var tokens = line.split(/\s+/);
process.argv.forEach(function(index){
index = +index;
var val = null;
if(index >= 0){
val = tokens[+index];
} else {
val = tokens[tokens.length + +index - 1];
}
if(val){
message += val + '\t';
}
});
if(message){
console.log(message);
}
});
I can install it fine, but when I go to run it I get a syntax error on the first line. This doesn't happen when I run from the directory.
I install it like this
> sudo npm install -g $(pwd)
then I try and run it like this.
> echo hi how are you | nawk 0 -1
/usr/local/share/npm/bin/nawk: line 1: syntax error near unexpected token `'fs''
/usr/local/share/npm/bin/nawk: line 1: `require('fs');'
Upvotes: 2
Views: 626
Reputation: 546
I needed to add this to the top of the index file
#!/usr/bin/env node
require('fs');
var readline = require('readline');
...
Upvotes: 8