npm list -g shows packages I haven't installed - node.js

npm ls -g shows a ton a packages and i havent installed any of them except 1 or two.
enter image description here
the package.json in my home directory:
{
"name": "yash",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {},
"devDependencies": {},
"scripts": {
"test": "mocha"
},
"keywords": [],
"author": "",
"license": "ISC"
}
the node_modules folder in my home directory is empty.
so what are all these files?

The -g flag will show global packages. These are packages which are not included in your package.json but are instead either packaged with your installation or were installed with npm install -g.
If the global packages you install have their own dependencies, then you'll also see those child dependencies listed. For example, you can see you installed nodemon, so you'll also see chokidar in the list, since nodemon requires that package to work.
If you want to ignore the child dependencies and see only the packages you directly installed, you can use npm list -g --depth 0.
You can read more about global packages vs. local packages

Related

Why is npm i -g module different from npm i -g --only=dev .?

Let's say we have this simple package.json
{
"name": "test",
"version": "1.0.0",
"description": "Testing",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"keywords": [
"tests"
],
"author": "JJ",
"license": "ISC",
"devDependencies": {
"mocha": "^8.2.0"
}
}
Basically we're looking to install mocha as a dependency for development, obviously. We run
npm i -g mocha
It's installed, and then
which mocha
works.
However, this does not yield the same result:
npm i -g --only=dev .
Obviously, it's installing things someplace else which is not in the path, possibly. But how come it's different? Running with --verbose does not yield much more information on where exactly it's going to be located.
Anyway, what I'm looking for is is a consistent way of specifying these global requirements. It does not seem too clean to create a Docker image or whatever with lots of npm i -g xxx yyy zzz followed by npm i with a package.json (or package-lock.json) where those dependencies are already specified. Maybe there's another way I've missed?
When you install an npm package with the -g tag, you are installing the package globally on your machine, not in your project;
Only use the -g tag if you are going to run something specific on your machine that isn't required by the project; otherwise, just hit npm i and install it directly on the project directory.
If it's going to be a development dependency, install it with npm install --save-dev mocha

NodeJS installed modules are not recognized on Windows 10

I need some help to setup NodeJS on a Windows 10 machine.
I installed the 10.15.3 LTS version from the official website which has done the following:
Node installed at C:\Program Files\nodejs\
Added to the Path in the user's variables: C:\Users\rsantos\AppData\Roaming\npm
Added to the Path in the environment variables: C:\Program Files\nodejs\
Created an empty npm folder at: C:\Users\rsantos\AppData\Roaming
Then with npm init I initialized the package file which was created at C:\Users\rsantos.
Followed by npm install supervisor which:
Created a npm-cache folder at C:\Users\rsantos\AppData\Roaming\npm-cache
Created a node_modules folder (with the supervisor) at C:\Users\rsantos\node_modules
Created a package-lock.json file at C:\Users\rsantos with:
{
"name": "rsantos",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"supervisor": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/supervisor/-/supervisor-0.12.0.tgz",
"integrity": "sha1-3n5jNwFbKRhRwQ81OMSn8EkX7ME="
}
}
}
Updated the package.json file to:
{
"name": "rsantos",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"supervisor": "^0.12.0"
}
}
Then when I open the command line and type:
C:\Users\rsantos\Desktop>supervisor --watch C:\NodeJS_Projects\Proj1\ -e js C:\NodeJS_Projects\Proj1\Test.js
I get:
'supervisor' is not recognized as an internal or external command, operable program or batch file.
After that I tried to install supervisor globally and it started to work.
The next error was 'require' not being recognized after installed locally.
I followed the same approach which made supervisor work and installed require globally. My code is requiring the module with require('request'). But even installed globally, it fails with this error:
Error: Cannot find module 'request' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
Notes:
I uninstalled everything and reinstalled, the result was always the same.
Also played with the environment variables but still no good.
npm list -g request outputs this: C:\Users\rsantos\AppData\Roaming\npm -- request#2.88.0
At this point my C:\Users\rsantos\node_modules folder has all the modules, including supervisor and request.
The package.json has supervisor and request as dependencies.
The package-lock.json seems to have all the modules in the node_modules folder, including supervisor and request.
I still get the Cannot find module 'request' error.
Can someone help please?
when you want to use a NPM package as tool (e.g. >supervisor --watch PATH), you can install the package --global. When you want to load the package in Node with require() I would install it locally.
Does the following work for you?
Install the tooling globally:
npm install --global supervisor
Set up your project in local folder:
mkdir project
cd project
npm init -y
npm install --save request # install to project/node_modules
node index.js # start your app

npm install saitek -g keeps installing wrong dependency

I am trying to install an sdk from https://github.com/niftylettuce/flightgear-saitek-pro-flight-switch-panel-hid-controller
the package.json looks as follows:
{
"name": "saitek",
"version": "0.0.2",
"main": "app.js",
"author": "Nick Baugh <niftylettuce#gmail.com>",
"license": "MIT",
"bin": {
"saitek": "./app.js"
},
"dependencies": {
"chalk": "^0.4.0",
"node-hid": "^0.5.4",
"underscore": "^1.6.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"description": ""
}
It clearly states it needs node-hid#0.5.4
When I check in my terminal (npm show node-hid version), it returns 0.7.3
As soon as I give the command:
npm install saitek -g
It starts with:
node-hid#0.3.2 install /usr/local/lib/node_modules/saitek/node_modules/node-hid
Clearly not the version I want. Especially as it gives all kinds of errors, which breaks the install.
I am on a MacBook Pro with Sierra as operating system.
The version of saitek on GitHub is more up-to-date; it has the node-hid dependency 0.5. If you install a package, npm will install the declared dependencies for that package. The last version of saitek released on npm is 4 years old and has an older node-hid dependency:
npm view saitek dependencies
{ ... 'node-hid': '^0.3.1' ...
It's up to the owner of the saitek package to publish a new version. You might try contacting them or getting in touch with npm support: https://docs.npmjs.com/misc/disputes
In the meantime you can install the package directly from GitHub since it has an updated package.json:
npm install -g https://github.com/niftylettuce/flightgear-saitek-pro-flight-switch-panel-hid-controller
This doesn't seem to work with node 10, but it does work with node 9 and 6 at least.
It's possible that npm install -g saitek will work with an even older version of node. It didn't work for 6 for me though.

package.json for minimal command-line utility

I am trying to create a command-line utility. But, npm install (no -g) does not link the executable.
My expectation is that npm install would install my executable locally.
My package.json looks like:
{
"name": "test-bin",
"version": "0.1.0",
"description": "Test bin",
"bin": "./bin/test-bin.js",
"main": "./index.js",
"author": "",
"license": "ISC",
"repository": {
"type": "git",
"url": "file:///tmp/test-bin.git"
}
}
index.js is:
module.exports = function() {
console.log('invoked')
}
bin/test-bin.js is:
require('../')()
If I run npm install, node_modules is created, but not .bin
However, if create another project elsewhere that uses the first as a dependency:
{
"name": "test-test-bin",
"version": "0.1.0",
"description": "Test test bin",
"author": "",
"license": "ISC",
"repository": {
"type": "git",
"url": "file:///tmp/test-test-bin.git"
},
"dependencies": {
"test-bin": "file:///Users/you/somewhere/test-bin"
}
}
then npm install links the executable in that project:
node_modules/.bin/test-bin
The npm documentation says, about "bin":
To use this, supply a bin field in your package.json which is a map of
command name to local file name. On install, npm will symlink that
file into prefix/bin for global installs, or ./node_modules/.bin/ for
local installs.
Is it as designed, or am I missing something?
Running npm install inside a package folder will install its dependencies, but it doesn't install any binaries that the package itself declares (you could argue what the point of that would be).
That only happens when the package is installed as a package (using npm install package-name or as a dependency for other packages).

npm install doesn't install any dependencies

I am trying to install packages in the package.json file. Unfortunately, when I run npm install, nothing happens (nothing is installed). I have used npm install on other repos and it works successfully.
Here is my path:
$PATH = /Users/me/.rbenv/shims:/Users/me/.rbenv/bin:/usr/local/share/npm/bin:/usr/local/bin:/Applications/Postgres.app/Contents/MacOS/bin:/usr/bin:/bin:/usr/sbin:/sbin
As you can see, npm/bin is in my bath and I believe this is correct.
Here are the instructions for this repo and where to run npm install (which I am doing)
go into "module"
run "npm install"
pair your laptop/pc with intelligent brick troubleshoot: http://www.ev-3.net/en/archives/97
run example.js: "node example.js"
see "example.js" for more details
When I run npm install in the module directory (that has the package.json) nothing installs.
Here is the package.json:
{
"name": "ev3-nodejs-bt",
"description": "Bt Api for lego ev3 robot",
"version": "0.0.4",
"private": false,
"dependencies": {
"serialport": "1.*"
},
"main": "Ev3.js",
"devDependencies": {"serialport": "1.*"},
"scripts": {
"test": "node Ev3.js"
},
"repository": {
"type": "git",
"url": "https://github.com/wenchaojiang/Ev3NodeJsBtAPI"
},
"keywords": [
"node.js",
"ev3",
"lego",
"robot",
"bluetooth"
],
"author": "Wenchao Jiang <psxwj#nottingham.ac.uk> (http://wenchaojames.wordpress.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/wenchaojiang/Ev3NodeJsBtAPI/issues"
}
}
Do I have something set up wrong on my system? (I don't think I do based on my $PATH and successful installing packets in other node-js repos) Is this package.json file not valid?
npm install doesn't install (or echo) anything when all of the dependencies are satisfied. Ensure there's a serialport folder under node_modules.
If you want to reinstall everything, you just need to remove the node_modules folder:
rm -r node_modules
npm install
If you have an npm-shrinkwrap.json file, check it. The npm install command will only install the packages specified in that file.
According to the npm install docs:
If the package has a shrinkwrap file, the installation of dependencies will be driven by that.
I had the same problem with my project. And when I looked at my npm-shrinkwrap.json file, I saw dependencies: {}. So that's why it didn't install anything for me.

Resources