Node fs.readdirsync read directory in npm package - node.js

i tried to use fs.readdirSync and it work perfectly, but when i publish to npm and install this package to my new project, it not read my package directory instead it will read from my new project directory.
My goal is to get list file names inside package directory not new project directory
const dirents = fs.readdirSync("./", { withFileTypes: true });
console.log(dirents)

You need to use __dirname because it is an environment variable that tells you the absolute path of the directory containing the currently executing file.
Instead of using ./ it will represent your current working directory

Related

Is it possible to make the node command see a node_modules folder on a different location than the specified one?

I know I can install Node.js libraries with the command npm from the command line... So, if I type:
npm install mylibrary
It'll create a node_modules folder on my current location and it'll install mylibrary (if it exists on npm)... Let's say that I create a Node.js code using it as the following:
const mylibrary = require('mylibrary')
mylibrary.doSomething()
Since I've installed this library on my current folder with the last command, this node.js script will work only if I save it on a file on my terminal current location as well. If I name this file as file.js, I can execute it with:
node file.js
Well, my problem is that I have a very specific case where the node_modules folder cannot exist in the same location that my file.js. Also, I'd like to avoid having to install mylibrary globally. Is there any way of defining as a parameter the path that the command node will search for the node_modules folder? I've checked node --help and it seems there's a lot of options, but I didn't manage to make it work. Is it possible to do? Can I use a node_modules folder that's neither the one of my file.js path or a global installed library?
You can have a link for the node_modules folder. For example, to create the symbolink link in Linux do this:
cd /path/to/script/folder
ln -s /path/to/where/you/can/have/node_modules node_modules
If you can't have the link, use the full path to the node_modules folder in the require, for example:
const mylibrary = require('/path/to/node_modules/mylibrary')
Also, you can solve the problem by using the NODE_PATH environment variable, set it like this
export NODE_PATH=/path/to/node_modules

get source folder when executing globally installed npm cli package

I am creating my own CLI using NodeJS. One task of the CLI is to copy folders recursively from a source folder within the project.
So, I install my CLI globally using npm install -g from within my CLI project folder.
Now I can run it in another test folder.
However, when I run it, the recursive copy fails because it is trying to find the files from the source folder in the test folder I created. Not from /usr/local/lib/node_modules/[myCli].
I have tried many solutions using path and require.resolve and __filename but they all give me a folder starting from the test folder in which I am executing my CLI.
Other then hardcoding the source path to /usr/local/lib...., there most be a way to get the folder of the globally executed script?
If you search for the current working directory its process.cwd() https://nodejs.org/api/process.html#process_process_cwd
__filename give you only path of file where it installed in node_modules
To find your project files you can use rc file in your home directory containing the path of your project
https://en.wikipedia.org/wiki/Run_commands
in hope this helps

Accessing cli global files with NodeJS

I'm developing a CLI module that can be installed either locally or globally. It references local templates to be used during the CLI process (e.g. copy some of them in the current folder, ...).
How can I retrieve the path where those templates are installed (in the current node_modules folder or in the global npm/yarn node_modules folder) to use them?
So the scenario is that a user installs your tool via npm install, and then that tool copy some templates -- from your project folder -- into the user current working directory (among other things).
So your template files may be installed in a local node_modules installation or in the global one.
In both cases, you can access the absolute path to a resource given a relative path from your script file, via the path module:
const pathToResource = path.resolve('../templates/myTemplate.txt');
pathToResource will be the absolute path and will change from some users to others, depending upon they installed the module locally or globally.
I don't know if it's the best solution but I manage to retrieve the binary path by doing : path.resolve(require.resolve('my-cli'), '../', 'templates')
Source: https://github.com/nodejs/help/issues/388#issuecomment-264731257

Force require to check the current directory for a module

I'm creating a command line application with Node. How can I make require check the current directory for the module?
For example, I want to run the command in the testing directory:
root#hello:~/projects/testing$ mycmd
And get the module which is in the node_modules/ of that directory, rather than it using a global version.
var myModule = require("testing/mymodule");
Using process.cwd() to find their current directory, I can then just get the dependencie from the node_modules/ folder.
/usr/local/lib/node_modules/mycmd/index.js:
require(process.cwd() + "/node_modules/request");

Determining Node modules location at runtime

I am building a tool which is basically a node.js module. This tool can be installed globally (with -g option) I have few static files in the module to generate a report. If the module is invoked locally, I can refer to the static files with relative path ./node_modules/<module>/static/filename. But when the tool is invoked as a command, how do I refer to the static files? And how can I determine whether the tool is invoked as a local module or as a command?
When your tool is installed globally, there are usually two entries on Unix systems:
/usr/local/lib/node_modules/<module>/ -> the directory containing all module source files
/usr/local/bin/<script.js> -> symlink to your module executable script
Because Node is using the real path (i.e. after all symlinks were resolved) to resolve a path relative to current module, you don't need to worry about differences between global and local install.
What you do need to handle is the possibly different current working directory where the node process is running. The solution is to resolve the relative path to your static file against the absolute path where your module resides, not to assume that the node process will run in a particular directory as you do in your example.
There are two ways for that:
Using __dirname (API docs), which contains the directory name (path) of the current source file:
var path = require('path');
// assuming this script is in package root directory
var staticFile = path.resolve(__dirname, 'static', 'filename');
// if the script is in lib/ subdirectory, then you want to call
var staticFile = path.resolve(__dirname, '..', 'static', 'filename');
Using require.resolve(), which returns the exact same filename as would be used by a call to require():
// assuming this script is in package root directory
var staticFile = require.resolve('./static/filename');
// if the script is in lib/ subdirectory, then you want to call
var staticFile = require.resolve('../static/filename');
Use the magic variable __dirname. It refers to the directory containing your script file.
http://nodejs.org/api/globals.html#globals_dirname

Resources