get source folder when executing globally installed npm cli package - node.js

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

Related

How to automate batch command file for globally installed package?

Good day!, I don't know if my question is correct. but what happens here is I created my own local development environment with the portable binaries. Please see:
Everything is working fine except for NPM. Everytime I install a global package in NPM I first need to create a batch file to call the .cmd from the node directory.
E.g. After installing Angular CLI, I need to create a batch file pointing to the ng.cmd file. Please see: (node directory)
And here is my ng.bat file in the cli folder: (cli directory)
The reason why I did this because I don't want to mess up my windows PATH environment settings. Here is my PATH variables where I already added my cli folder:
Is there a way of automating these? instead of creating manually a batch file in my cli folder every time I add a new package or binaries?
Best Regards

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

How are relative paths resolved inside of node packages?

I am making a npm package which, after installation, runs a postinstall script located in the lib/ directory. This postinstall script in turn copies a config file, located in the same directory (as the module), to the user's project's root using fs.copyFile function. But I am a little confused about how the two paths are referenced when from inside the node_modules directory.
So, long talk made short, I want to figure out what prefixes should I use to refer to files in MY module and for the user's project's directory. Basically, how are relative paths resolved by node when referenced from a script inside the node_modules directory?

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

Node.js how to find local node_modules folder from a globally installed package

I'm writing my own CLI node module 'npak' that is installed globally.
I need to find the absolute path of the local 'node_modules' folder of a project where the (the global) cli command was executed.
say the project looks like this:
/home
/projects
/test-project/
/package.json
/node-modules
/folder-one
/folder-two
/folder-three/ npak find-modules
^-- this should return absolute path of node-modules folder
should return : /home/projects/test-project/node-modules even though npak is installed globally
How do I get at the absolute path of the local 'node-modules' folder regardless of what the cwd is from a globally installed package?
There are some useful modules, like find-node-modules, that can help you with such a task.
__dirname gives the path of the executing script.

Resources