2

I'm trying to create a node module that has the ability to be installed globally using npm install -g mymodulename. I've got everything in the module working fine if I run node index.js in the directory of the module, but now I want to make it so that I can publish it to NPM, and it can be installed and run from any directory.

There is some code in my module that looks at certain files in the directory that it is run in. I'm finding that when I do npm install -g ./ and then go into a different directory for a test, then run my-module-command, the relative path that it is reading is from that of where my module got installed (i.e. /usr/local/bin/my-module), not the directory that I'm running it in.

How can my module that is installed globally know where it is being run from? To give an example, I am trying to read the package.json file in the directory I'm in. And it is reading the package.json file of /usr/local/bin/my-module/package.json

I've tried:

__dirname
process.args[1]
process.cwd()

And just calling straight to require('./package.json') directly and none of those work.

Edit here's some code that's breaking in index.js:

#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var currentDir = path.dirname(require.main.filename);
fs.exists(`${currentDir}/node_modules`, function(dir) {
  if (!dir) throw 'node_modules does not exist';
  // do stuff
});

In my package.json:

...
"bin": {
  "my-module": "./index.js"
},
...

I try to do npm install -g ./ in the project directory, and then I cd into a different directory called /Users/me/Projects/different-project, where another npm project is, and run my-module, and I get node_modules does not exist. When I log out currentDir, I get /usr/local/lib/node_modules/my-module where I'm expecting is to see /Users/me/Projects/different-project.

Joshua Terrill
  • 1,995
  • 5
  • 21
  • 40
  • Yeah, those methods had no right to work the way you wanted, since they find the directory of your current running script, which is that module. Havr you tried: `var path = require('path'); var appDir = path.dirname(require.main.filename);`? I think this should return the application's entry point. – Michał Szydłowski Mar 26 '17 at 09:48
  • When I log out `appDir` it shows me `/usr/local/lib/node_modules/my-module`, not the directory it's being executed from :/ – Joshua Terrill Mar 26 '17 at 10:12
  • 4
    `process.cwd()` should work. This is an example of almost exactly what you're trying to do (granted for a different file name): https://github.com/TechnologyAdvice/DevLab/blob/master/src/config.js#L10 – Fluidbyte Mar 26 '17 at 21:04
  • Very helpful question, and answer from @Fluidbyte - I upvoted another of your answers to say thanks ;-) – davnicwil Aug 31 '19 at 18:57

1 Answers1

0

Have you tried using ./ at the start of your file path? That should give you the current working directory (or calling process.cwd() would work too).

In your case, your code would look like:

fs.exists(`./node_modules`, function(dir) {
  if (!dir) throw 'node_modules does not exist';
  // do stuff
});

I can see some comments already mention this. Maybe I'm misunderstanding the question? I just had a case where I needed a global module to get the directory I'm running the script from and what I suggested above worked like a charm.

Caleb Waldner
  • 889
  • 8
  • 11