how do I find the project folder location in a bin script?

65 views
Skip to first unread message

Mark Hahn

unread,
Aug 3, 2013, 3:24:20 PM8/3/13
to nod...@googlegroups.com
I'm using an npm bin config to create a symlink to a script in my project folder.  When that bin script runs, it has a working directory of the dir the symlink is in, not the project folder.  How do I find the project folder location inside that bin script?

Joel Brandt

unread,
Aug 4, 2013, 9:14:35 AM8/4/13
to nod...@googlegroups.com
By "project folder", I assume you mean "the folder the user is currently at in their terminal." If so, use process.cwd():

Hope that helps,
  -- Joel

Mark Hahn

unread,
Aug 4, 2013, 1:12:25 PM8/4/13
to nod...@googlegroups.com
process.cwd() points to the location of the symlink, not the bash script in my folder.

I'm using this code for now.  It uses process.cwd() to find the directory containing of the symlink, then adds the filename to that to get the path of the actual symlink file, then resolves the symlink to find out where it points to,  then strips off the end of the path to find the module folder location.  (My module is called bace).

SYMLINKTARGET=$(dirname $0)
SYMLINKTARGET+=/bace
BACE_DIR="`realpath $SYMLINKTARGET`"
BACE_DIR=$( dirname "$BACE_DIR" )
BACE_DIR=$( dirname "$BACE_DIR" )
cd $BACE_DIR

This seems like a lot more work than should be necessary.  Also realpath is not available in the linux distributions.  So the user will have to install that.  This script will be called a lot in tight loops so I'm also worried about performance.

I was hoping that this is a common problem that npm would somehow solve for me.



--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Floby

unread,
Aug 5, 2013, 4:40:58 AM8/5/13
to nod...@googlegroups.com
I assume your module structure looks like this

/bin/myBin.js
/lib/myUtil.js
index.js
package.json


/// index.js
exports.util = require('./lib/myUtil')
exports.projectDir = __dirname


/// bin/myBin.js
var dir = require('../').projectDir


Does that do what you want?

Mark Hahn

unread,
Aug 7, 2013, 10:15:54 PM8/7/13
to nod...@googlegroups.com
I finally figured this out.  If I use a node script, "#!/usr/bin/env node", instead of a bash script, "#!/bin/bash",  then the node script can simply reference __dirname. It was really hard and messy using bash.  I guess this is what several of you were trying to tell me.

--

Floby

unread,
Aug 8, 2013, 4:31:39 AM8/8/13
to nod...@googlegroups.com
Well first off, you never mentionned you were using bash.

But, I agree it's way easier in node


On Saturday, 3 August 2013 21:24:20 UTC+2, Mark Hahn wrote:
Reply all
Reply to author
Forward
0 new messages