Sepp Renfer
Sepp Renfer

Reputation: 131

npm bin property is not working as expected

I wrote a simple 'addheader' node module and published it on npm. It contains a cli script which I registered in the package.json config-file in the bin property as follows:

{ 
   "name" : "addheader", 
   "bin" : "./addHeaderCli.js" 
}

Installing the module with "npm install addheader" I can see the cli script in the node_modules folder as follows:

node_modules/.bin/addheader

Unfortunately I can't use the cli as expected. When run it like this:

node addheader 'test.txt' 'A header'

It always fails with the following error:

Error: Cannot find module '/Users/sepp/camp/npmbuild/addheader'
 at Function.Module._resolveFilename (module.js:336:15)
 at Function.Module._load (module.js:278:25)
 at Function.Module.runMain (module.js:501:10)
 at startup (node

Run it providing the path works fine:

./node_modules/.bin/addheader 'test.txt' 'A header'

I've consulted the npm documentation but I couldn't figure out what I'm doing wrong.

By the way I'm running node version 0.12.4 on a mac.

you can find the module on npmjs https://www.npmjs.com/package/addheader

Upvotes: 1

Views: 4656

Answers (2)

Sepp Renfer
Sepp Renfer

Reputation: 131

I solved the problem by adding the following line to the top of the addHeaderCli.js:

#! /usr/bin/env node

Now I can cal it like:

addheader 'test.txt' 'my header'

Upvotes: 3

pllee
pllee

Reputation: 3959

If you create a bin you should not be invoking it with node it it just a standalone binary.

karma

Command not specified. Karma - Spectacular Test Runner for JavaScript.

node karma

module.js:338 throw err; Error: Cannot find module

Upvotes: 2

Related Questions