How to automate batch command file for globally installed package? - node.js

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

Related

How to download npm packages instead of using the command line

I'm working in a completely offline environment & lately, I've had to code some react applications. I wonder if there is a way to download npmjs packages manually without using the command npm install <package-name>.
For example, while I'm coding with python in the same offline env, I'm downloading everything from PyPi manually & use the .whl in my offline environment using removable devices from one online environment to the offline environment.
I'll appreciate for any help or direction to the solution.
You need to download their source from the github. Find the main file and then include it in your main file.
You need to find the source and go through the package.json file. There you can find which is the main file. So that you can include that in your application.
To include example.js in your app. Copy it into your application folder.
Once you do that, your requirements will just be:
var moduleName = require("path/to/example.js")
It will always look for a node_modules directory at the root of your app (and a few other places as well).
It's important you download the full repo and not just the lib folder if you plan to use it this way since the package.json file is used to find the main entry point. As long as the repository has a valid package.json file it should work.[

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

How to generate JHipster application into a specific directory?

Suppose I modified the source code of generator-jhipster repo and I would like to generate my "custom" JHipster application into a specific directory, how would I accomplish this task?
After installing JHipster:
npm install -g generator-jhipster
I would be able to generate the application with:
jhipster
However, this command runs the application directly from the JHipster repository.
Since I modified the source code, I could generate the application locally by running:
node jhipster.js
in the ../cli directory.
The problem is, running this command in cli directory will generate the application in the same directory (cli) and this is not what I want.
I need to find a way to export the application into a specific directory.
Note: I strongly believe this has something to do with process.cwd(), because that generates the application in the local directory, but I'm not sure exactly
which source file to modify as I don't want to break anything.

Why does VS 2017 set node_modules folder to read only in .Net Core App?

I'm building a .Net Core application using Angular for my client-side code. For the most part, I'm using the default template that is included in VS 2017. For whatever reason, VS is making my node_modules folder read only. Before I was able to install packages via command line in the directory that holds my client side code as well as my package.json file and my node_modules folder. Before I was able to do this, but now it is defaulting the folder to read only which is invaliding all of my npm commands. I've verified that this is the case because I can remove the read only attribute via windows explorer and then run any of my commands like npm install.
Has anyone else encountered this before? If so, what did you do to resolve this?
Thanks!
Okay, I found the answer. VS puts a lock on the node_modules folder while it is running.
So, I guess for now if you need to add packages just close VS first.

Resources