Using npm as a library - node.js

I have been trying to understand how to make npm work as a library for quite a long time now.
Here is what I have been doing, but it feels quite evident to me that this isn't the proper way to do such a thing. Say that I want to install the library sqlite3.
Here is what I have been doing:
mkdir npmtest
cd npmtest
npm init
npm install --save npm
node
And inside node, I just do
var npm = require('npm');
npm.load(function()
{
npm.install('sqlite3')
});
It does... things. Like it shows a progress bar in the terminal, then it just crashes with a huge amount of errors. I have pasted them here.
I am evidently doing something wrong. How can I get this working? I need to be using npm as a library, I cannot make use of the command line tool, since I will be distributing my app with Atom Electron, and I cannot assume that npm will be installed where I deploy my app.

Related

how to install nodejs and npm on linux and have a working semver module

after trying to update nodejs because there was a problem with the semver module it was not working anymore. searching the internet revealed that there is something wrong with the installation. after removing nodejs as well as npm and installing them again i cannot run npm anymore as the binary is not found.
as far as i can tell there are multiple ways to install nodejs and npm and i am wondering which is the best way. i would prefer a method that does not rely on a package manager. also i might have trace amounts of previous nodejs installations left and i don't know how to get rid of them.
i don't know if this is important, but i need npm for a laravel project that is using vue. without it i would never have touch it.
thank you very much for any help!

Why does one npm Module delete other npm modules?

This whole "nvm" - "npm" fiasco is a disgusting mess. For one thing, they should have a big flashing red banner at the top of the npm Web-Site that says, "If you intend to do ANYTHING with Node.js, you better decide right now to get rid of the spaces from your folder names." I never saw any warnings to sanitize my pathnames. And it didn't help that I tried to go back later and delete those blank spaces out of the path. As far as npm is concerned, I committed a capital offense with those blank spaces, and I have paid dearly for that error.
For all I know, maybe I am still paying. Why is it that when you install "nodemon" as a development dependency npm install --save-dev nodemon ( as opposed to just installing it globally in a totally separate folder npm install -g nodemon ) why does it delete "npm"? Oh, the npm files are still sitting in the same place that they have always been, but when you go into the Command Prompt and type npm -v the Terminal acts like he never heard of npm ... like you must be speaking in Russian.
OK, so let's use the Node.js installation executable to "RE-install" the npm that has gone on sabbatical ( actually, the *.msi file calls it "repairing" the installation. ) Now, you get your npm back. Great. But now you have a new "npm" folder with hundreds of "node_modules" sitting in a sub-directory of npm. This is what they call repairing?
But your troubles aren't over yet. Let's install the very popular npm module: "dotenv" as a development dependency. This time, the "nodemon" folder ( that you just installed 1 minute ago ) has been deleted, and yes, ( yet again, ) npm has gone fishin'. That is to say, npm -v no longer works, even though the npm files haven't gone anywhere.
I would love to know. How many times am I going to have to re-install npm before I finish writing my first childishly simple Node.js Module?
It's a good thing the npm trash is free. They couldn't afford to give us our money back.
I will probably be crucified for answering my own question, but it is working for me.
Here is the bottom line: Everyone should be able to reasonably expect that when they install a software package, that it is not going to cannibalize other software that they have installed, nor is it going to chew-up their operating system. But that is what was happening to me with this npm trash.
And if you want to know if you are having the same problem, just run this test:
Install npm and Node.js Then, install about 5 more npm modules of your choice.
After the installation is complete, you should immediately UN-install all 5 npm packages, plus Node and npm.
After the installation and Un-installation is finished, you should be left with an empty folder. If the folder has ANYthing left, then your npm modules have been chewing on each other, and spitting out the pieces for you to have to clean up.
That is what was happening to me. And I suspect that whenever you hear someone on YouTube suggesting that you should install Nodemon 'Globally', my guess is that they are having the same problem that I had, but they don't know how to fix it, so they just push Nodemon off into a different folder where it can't destroy the other modules that they have already installed.
But their "solution" is actually not such a bad idea. Why not put EVERY npm module into its own separate folder ? You could sandbox each package, to protect it from other packages that you install later.
To make my solution clear, I will diagram my proposed folder structure. We start by installing npm and Node:
Node
|
|__node_modules
Now, npm wants you to continue installing all other npm packages into the node_modules folder. But you can force npm to put each npm package into a folder that you get to pick, so that the new structure looks like this:
Node-14
|
|
|_____myExpress
| |
| |__node_modules
|
|_____Joi-13
| |
| |__node_modules
|
|
|__node_modules ( should be NODE ONLY ...
nothing else. )
Here is the command that will allow you to achieve the outcome above. In the Command Prompt, you must first Navigate your way into the Node-14 folder shown above. Once you are in that folder, type the following command:
npm install --prefix ./myFolder npm-package
npm will happily create the folder called "myFolder", and then will install the latest version of the "npm-package" that you requested.
And here are a few examples:
npm install --prefix ./myExpress express
npm install --prefix ./NodeMon-dev nodemon
npm install --prefix ./Joi-13 joi#13.1.0
In the examples above, I added the -dev suffix onto the NodeMon folder name, to signify that this package is for Devlopment Purposes.
Also, the 'joi' example shows you how to intentionally install an old version of an npm package, for whatever reason.
There is one caveat to the instructions that I have given. I have said that you can choose whatever folder name you want. That is not 100% true. If you choose a folder name which is the exact same as the package name, then you run into a problem. For example,
npm install --prefix ./passport passport
If you type the command above, then npm will tell you that 'passport' is already installed, and is up to date.
That, of course, is a lie.
Just another example of npm talking trash.
But just to be painfully clear on this issue, in the command below:
npm install --prefix ./Passport passport
The folder name, 'Passport' is NOT exactly the same as the npm Package 'passport' ( because of the Capital Letter ), so that command will work fine.
In the interest of full disclosure, I must admit that there is a price to be paid for using the installation strategy above: when you "require" an npm package in the Node.js software that you are writing, it will no longer be short and sweet:
const express = require('express');
Instead, you are going to have to 'search' for each module individually, because each one is hiding in the sandbox that you placed it in:
const express = require ( '../Node-14/myExpress/node_modules/express' );
Depending on how deep your folder structure goes, it could easily be even worse that what I have shown. In some cases, you might even have to go up 2 or 3 levels from where you are sitting:
const express = require ( '../../../Node-14/myExpress/node_modules/express' );
But in my case, after installing and uninstalling hundreds of npm modules, this was a very small price to pay, for the comfort of knowing that you are not going to be sabotoged by yet another poorly-written pile of trash that acts like a playground bully.
I guess I should also admit that deleting Modules might be a tiny bit more complicated than you are used to.
If you go into the Command Prompt and Navigate to the Node-14 folder ( as you did when you installed the various npm Modules ), and then if you type the customary npm uninstall <npm-package>, you can watch while npm furiously fills up your screen with reams of paths and commands, looking like it is actually doing something.
That, of course, is another lie from npm. In truth, no action was taken.
To uninstall the package, you must Navigate to the Sandbox Folder that you selected, and THEN type the same command. A couple of examples :
Navigate to the "myExpress" folder, and type npm uninstall express
or Navigate to the "Joi-13" folder, and type npm uninstall joi
You will be delighted to find that when npm finishes with the un-install process, the folder will be empty, except for an occasional JSON file. This was a huge change from what I was used to seeing.
Now, you might think that I have gone on and on about npm in this posting, and you might be right. But the truth is, I have left a LOT out of this answer that there is not room to talk about.
For example, I have another mysterious issue going on with my computer that I have posted in a question here:
Running the contributed command: 'code-runner.stop' failed
I seriously doubt that anyone is going to attempt to answer that question, so I will probably have to answer it myself someday. And I predict, that when I finally find out what caused that bizarre issue that I described in the question, that it is going to have something to do with npm. That is my prediction, because I have nothing good to say about npm.

How to use NPM programmatically?

After seeing this post and other resources, I wanted to try using NPM programmatically so simply I tried:
const npm = require('npm');
but I run into an error of Error: Cannot find module 'npm'.
I did some research and all I saw was to try running npm install npm#latest -g which just updates NPM if I'm not mistaken but I ran it anyways and no change. NPM is already installed globally and I did try installing it locally which got rid of the error but broke the rest of my project so I assume there's a reason that wasn't recommended.
All of the posts I've seen have been older so is this not supported anymore? All the documentation I can find is about the cli and not using it as a module.
Use
$ npm i npm
without the global -g Flag
Note: The NPM API is not intended for external use (even tho it is somewhat documented). The problem is, the NPM API does not guarantee stability. In fact, it doesn't even follow semantic versioning. An update to the NPM API is likely to break your script if you heavily rely on it.
If you want to use NPM externally, it would be better to use it as it is right now: a command-line program.

Do I have to learn node.js in order to use npm?

I need to use a package on npm, so I'm trying to learn how to use npm. The tutorials are fine, but I feel like they're assuming I know node.js, which I don't, and I'm having a hard time finding a tutorial for npm that doesn't also assume I know node.js. Do I need to learn node.js to use npm?
you don't need to know anything about node to use npm, its just a package manager. Install npm and then npm install all the packages you want. You will need to learn the npm toolchain, however, and it also helps to know which options are available for the various commands.
At the very least you should know the difference between installing a package globally and installing a package locally, i.e npm install -g vs. npm install respectively.

npm path and installation issues - suggestions

I have used to install my nodejs on D:\ drive instead of C and have set environment variables to D drive node & npm folders.
Then i changed npm installation path as "prefix=D:\node\node_modules\npm
" on "npmrc" file. So i could confirm that all user based modules are pointing on D drive npm folder instead of appdata.
I tried to install express js globally and i used to check the package tree on my cli as mentioned below,
npm ll -g
while trying this command am getting npm extraneous ERR,
Please suggest me that which way i have to use npm path and installation stuffs.
Thanks in advance.
It might seem like a good idea to install packages globally, but this is one great reason not to.
Often used packages like express, and cookies should be kept local to a package. Mostly because of versioning issues. You might have one package using express2, but your new one wants to use express3. You would have trouble if it was a global install. When in doubt leave off that -g, and use a --save instead. (This adds the package to your npm dependencies list.)
On the other hand, command line tools like mocha, yeoman, and uh not much else that I know of should be installed with the -g flag.
I'm not much of a windows person, so you'll have to look a little yourself, but I would also recommend not installing Node by hand, but instead using a version manager like nvm to do that stuff. Here's an nvm port for windows: https://github.com/coreybutler/nvm-windows

Resources