Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8473985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:30:07+00:00 2026-06-10T17:30:07+00:00

I am creating a node command line interface. It is installed globally and uses

  • 0

I am creating a node command line interface. It is installed globally and uses a bin file to execute.

I plan to have a command window open at the root directory of the files I am working on and then just run the command however I have been unable to determine the current working directory as process.cwd() is returning the directory of the node package. I initially assumed that since the code is being executed using a batch file as a wrapper (that is how bin files can execute without node at the beginning) then it is impossible but coffee-script manages to do it. I took a look at the coffee-script source but couldn’t follow it (not experienced enough).

To test it for yourself create a package with this package.json file:

{
  "name": "test-package",
  "version": "1.0.0",
  "bin": {
    "test-package":  "./bin/test-package"
  },
  "main": "/lib/test"
}

this test-package file in bin:

#!/usr/bin/env node

var path = require('path');
var fs   = require('fs');
var lib  = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');

require(lib + '/test');

Could anyone shed some light onto this.

and then try and get the command line directory inside lib/test.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:30:08+00:00Added an answer on June 10, 2026 at 5:30 pm
    • process.cwd() returns directory where command has been executed (not directory of the node package) if it’s has not been changed by ‘process.chdir’ inside of application.
    • __filename returns absolute path to file where it is placed.
    • __dirname returns absolute path to directory of __filename.

    If you need to load files from your module directory you need to use relative paths.

    require('../lib/test');
    

    instead of

    var lib  = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
    
    require(lib + '/test');
    

    It’s always relative to file where it called from and don’t depend on current work dir.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a taxonomy attached with my content type. While creating node of that
I am creating a Node.js app on Heroku and have run into an issue.
If i'm creating the RMI registry from command line , the client has no
I am creating node.js app using express, everyauth and now.js. I have a server-side
I have a problem with Oracle Spatial Network package. I'm creating NODE, LINK and
I'm creating a node.js project that uses express.js and jade. I'm trying to reate
I am creating a node by using crrm as below $(#TreeDiv).jstree(create, $(#somenode), inside, {
I'm using drupal 7.x and am creating a node content-type template. My content type
how can I show users success/error messages without creating a node for it? Thanks
When creating a trivial Arbor JS graph with a single node, the node jitters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.