How to use NPM programmatically? - node.js

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.

Related

Is it necessay to update npm to update node?

I am new to npm and node so pardon if my question is silly, but is it necessary to update npm to its latest version if i want to update Node.js to its latest version? I read the npm wikipedia page and it says npm is a package manager for Node.js. Also, does npm provide a runtime environment for Node appications to work?
Node.js is the runtime (using the V8 JavaScript engine). Yes, npm is a package manager that helps include dependencies in the program, but it is not the only one. There are others like yarn, so npm is not essential to Node.js. There are several upgrade methods to choose from, using all the same installation methods typically available on operating systems. npm does not offer Node.js upgrades directly; options include node version manager and the npm-installable module n:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
None of the methods mention an npm update, though it is a good idea to keep npm current for security. If a given upgrade method does require you to have a certain version of some manager or installer, you will get a message about it when trying to upgrade.

Installation of a scoped package resolve in an E404

So I came across this problem when I am trying to install the request-promise module via npm.
It depends on a scoped package named #request/promise-core, and when I try to launch npm install request-promise from my project directory it returns me an error 404. This apparently it is a known issue, see: https://github.com/request/request-promise/issues/137, but none of the solutions works for me, probably due to a wrong implementation on my side.
What I tried so far:
configured npm config set "#request:registry" https://registry.npmjs.org/, but did not worked. Still returning E404
tried to manually install the package via github with npm install https://github.com/request/request-promise but no joy since it returns error: ENOEN when trying to open the request-promise-unpack/package.json file.
Probably I am missing some basic steps when trying to install the module, but I cannot figure out a different way of installing that module.
Any suggestions or alternatives is very appreciated.
I fixed with
npm install -g npm
in the terminal and then I could install it:
npm install --save request-promise

Using npm as a library

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.

npm installs not available in CasperJS

According to the CasperJS Documentation, I should just be able to install things through npm and then require them in my CasperJS scripts for use (since 1.1 apparently, and I'm using 1.1.0-beta3)
However, this doesn't work for me and I get the error "CasperError: Can't find module ModuleName", where I've tried read, underscore, readline, etc. And, they've all been installed using sudo npm install ModuleName.
Is there something I'm missing?
EDIT: For reference, this is related, but not identical, to my question here.

How to edit global file installation

I just ran this line of code
npm install -g csslint
CSSlint seems great but I want to add the ability to prettify my css by putting in custom code. The goal is to have a command line interface where I can format my css before pushing the code to the server.
"-g" stand for installing globally correct?
Where does this library get installed to and how do I access it? I thought it would be in my user/local/bin but I couldn't find it.
Thanks!
use npm root -g to see where modules get installed. If you want to use it in your code using require() then install it locally i.e. without using -g option. More details here https://npmjs.org/doc/faq.html
I believe it's going to depend on your npm settings, but my global npm binaries are in /usr/local/share/npm/bin

Resources