最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Binary file in npm package - Stack Overflow

programmeradmin0浏览0评论

I try to create an npm package, which can be started as a command from shell. I have package.json

{
  "name": "myapp",
  "version": "0.0.6",
  "dependencies": {
    "async": "",
    "watch": "",
    "node-promise": "",
    "rmdir": "",
    "should": "",
    "websocket": ""
  },
  "bin": "myapp"
}

and myapp

#!/bin/bash

path=`dirname "$0"`
file="/myapp.js"

node $path$file $1 &

But I get an error:

module.js:340
    throw err;
          ^
Error: Cannot find module '/usr/local/bin/myapp.js'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

The problem is that myapp.js is in another directory. How can I get this directory name from my script? Or maybe there is better way to do this?

I try to create an npm package, which can be started as a command from shell. I have package.json

{
  "name": "myapp",
  "version": "0.0.6",
  "dependencies": {
    "async": "",
    "watch": "",
    "node-promise": "",
    "rmdir": "",
    "should": "",
    "websocket": ""
  },
  "bin": "myapp"
}

and myapp

#!/bin/bash

path=`dirname "$0"`
file="/myapp.js"

node $path$file $1 &

But I get an error:

module.js:340
    throw err;
          ^
Error: Cannot find module '/usr/local/bin/myapp.js'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

The problem is that myapp.js is in another directory. How can I get this directory name from my script? Or maybe there is better way to do this?

Share Improve this question asked Jun 6, 2014 at 13:16 ciemborciembor 7,33714 gold badges61 silver badges103 bronze badges 4
  • Why dont you give that myapp.js directory path on cmdline call the script as ./scriptname.sh 'directory_path'and then instead of node $path$file $1 & use node $1/${file}, if you do this you wont need path=`dirname "$0"` – PradyJord Commented Jun 6, 2014 at 13:27
  • Because this file is part of a package. And I want this script to be a simple, global command. – ciembor Commented Jun 6, 2014 at 14:49
  • You'll need to resolve the $0 symlink path into the actual file path. Have you looked at stackoverflow.com/questions/4774054/… ? – loganfsmyth Commented Jun 6, 2014 at 16:03
  • Yes, I know, but I don't know where the myapp.js is, it is installed with a package and should be in different directories. The question is how can I find path to this file. – ciembor Commented Jun 6, 2014 at 16:12
Add a comment  | 

2 Answers 2

Reset to default 29

Actually, you can put your myapp.js file into bin.
So, the bin key in package.json file should be like this :

"bin": { "myapp" : "<relative_path_to_myapp.js>/lib/myapp.js" }

At the first line in myapp.js, you must add this shebang line :

#!/usr/bin/env node

It tells the system to use node to run myapp.js.


... Or if you don't want to call myapp.js directly, you can create a script like this to be your executable file :

#!/usr/bin/env node

var myapp = require('<relative_path_to_myapp.js>/myapp.js');
myapp.doSth();

and in package.json :

"bin" : { "myapp" : "<relative_path_to_the_script>/script.js" }

By doing this either way, you can avoid finding the path to your nodemodule.


But... if you insist to use your old myapp bash script, then you can find the path to the module with this :

myapp_path=$( npm explore -g myapp -- "pwd" )

Hope these help :D

https://docs.npmjs.com/files/package.json#bin

From the above link:

To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.

For example, myapp could have this:

{ "bin" : { "myapp" : "./cli.js" } }

So, when you install myapp, it’ll create a symlink from the cli.js script to /usr/local/bin/myapp.

发布评论

评论列表(0)

  1. 暂无评论