Determine command line working directory when running node bin script - node.js

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.

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.

Current Working Directory
To get the current working directory, you can use:
process.cwd()
However, be aware that some scripts, notably gulp, will change the current working directory with process.chdir().
Node Module Path
You can get the path of the current module with:
__filename
__dirname
Original Directory (where the command was initiated)
If you are running a script from the command line, and you want the original directory from which the script was run, regardless of what directory the script is currently operating in, you can use:
process.env.INIT_CWD
Original directory, when working with NPM scripts
It's sometimes desirable to run an NPM script in the current directory, rather than the root of the project.
This variable is available inside npm package scripts as:
$INIT_CWD.
You must be running a recent version of NPM. If this variable is not available, make sure NPM is up to date.
This will allow you access the current path in your package.json, e.g.:
scripts: {
"customScript": "gulp customScript --path $INIT_CWD"
}

path.resolve('.') is also a reliable and clean option, because we almost always require('path'). It will give you absolute path of the directory from where it is called.

Alternatively, if you want to solely obtain the current directory of the current NodeJS script, you could try something simple like this. Note that this will not work in the Node CLI itself:
var fs = require('fs'),
path = require('path');
var dirString = path.dirname(fs.realpathSync(__filename));
// output example: "/Users/jb/workspace/abtest"
console.log('directory to start walking...', dirString);

Warning if using ES Modules
According to the ECMAScript modules docs:
CommonJS variables __filename or __dirname are not available in ES modules.
Instead, use cases can be replicated via import.meta.url.
So according to why is __dirname not defined in node?, you can do the following:
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

Here's what worked for me:
console.log(process.mainModule.filename);

Related

Relative file paths in a global npm package

I have a command line utility that's installed as a global NPM package (privately, within our org).
I've noticed that file paths aren't actually relative to the installed location, but to the current working directory. In CommonJS I used __dirname but that's not valid with ESM.
So, when a user installs the package globally, and runs it, the following code errors because it's trying to use the current working directory.
fs.readFileSync('./templates/controller.ejs', 'utf8')
It works fine when I run the script from within the npm package folder itself. How I can always be sure it points to its install location?
EDIT: So far, the only way to do this is:
import { fileURLToPath } from 'url';
const dirname = fileURLToPath(new URL('.', import.meta.url));
const file = path.resolve(dirname, 'templates/controller.ejs')
fs.readFileSync(file, 'utf8')

Get `lib` path of a globally installed node CLI tool

I have a node cli tool I'm developing called wiki-sync.
I need access to the path to the node_modules lib dir from within my node cli app at runtime. This path, in my case, looks like: /Users/mchpatr/.node_modules/lib/node_modules/#parm/wiki-sync/
I've tried many things and none of these seem to give me what I am looking for. I'm certain there's an easy way to do this and I'm just missing it.
Here's what I've tried from within bin/wiki-sync:
process.cwd() /Volumes/Unix/workplace/Parm/src/Parm
process.execPath /usr/local/Cellar/node/13.8.0/bin/node
process.argv[1] /Users/mchpatr/.node_modules/bin/wiki-sync
fs.realpathSync(process.argv[1]) /Volumes/Unix/workplace/Parm/src/Parm/dist/apps/wiki-sync/bin/wiki-sync
which wiki-sync /Users/mchpatr/.node_modules/bin/wiki-sync
require.resolve('#parm/wiki-sync') will return the path to #parm/wiki-sync.
__dirname or __filename will return the path to the files directory or the directory itself.

Node global module getting current run directory

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.
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.

How does NPM/Node.js do path resolution in the local node_modules/.bin directory

Been confused about this for awhile
I have an executable file (cli/exec.js) in an NPM module called "foo" like so:
/foo
--/cli
exec.js
--/lib
package.json
in package.json, we have:
"name": "foo",
"bin": {
"exec-foo": "cli/exec.js"
}
when this module gets installed, with npm install, node_modules looks like this:
--/node_modules
---/.bin
exec-foo
---/foo
---/lodash
---/async
---/whatev
.........
...What confuses the crap out of me: how does Node.js do module/dependency resolution with the require function, now that the exec-foo.js file is no longer where it once was in the project, but is now located in node_modules/.bin? Is there a simple explanation for this?
NodeJS resolution is pretty well explained in the documentation.
If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node will not append node_modules to a path already ending in node_modules.
In your case node_modules/.bin/exec.js will be a symbolic link - a pointer to the original file.
Then NodeJS checks the original file path and starts parsing the required modules. If it can't find in the current node_modules path goes 1 directory upwards until it finds your library.
If no library has been found it throws an error.

NPM unable to read file

I have written a NPM module. which needs to read a file. While creating NPM module I have placed a file in same directory with .js file and used
var file = './sqlmap_config.json';
This files gets included well when I use the code standalone. When I create NPM module, this file is looked up in parent directory where NPM module is installed and code fails.
Do I need to hard code it to
var file = './node_modules/module/sqlmap_config.json';
Or there is any other way to accomplish this
I think you are going to want to use something like :
var file = __dirName + '/sqlmap_config.json';
https://nodejs.org/api/all.html#modules_dirname

Resources