Print a list of all installed node.js modules - javascript

In a node.js script that I'm working on, I want to print all node.js modules (installed using npm) to the command line. How can I do this?
console.log(__filename);
//now I want to print all installed modules to the command line. How can I do this?

If you are only interested in the packages installed globally without the full TREE then:
npm -g ls --depth=0
or locally (omit -g) :
npm ls --depth=0

Use npm ls (there is even json output)
From the script:
test.js:
function npmls(cb) {
require('child_process').exec('npm ls --json', function(err, stdout, stderr) {
if (err) return cb(err)
cb(null, JSON.parse(stdout));
});
}
npmls(console.log);
run:
> node test.js
null { name: 'x11', version: '0.0.11' }

list of all globally installed third party modules, write in console:
npm -g ls

in any os
npm -g list
and thats it

Generally, there are two ways to list out installed packages - through the Command Line Interface (CLI) or in your application using the API.
Both commands will print to stdout all the versions of packages that are installed, as well as their dependencies, in a tree-structure.
CLI
npm list
Use the -g (global) flag to list out all globally-installed packages. Use the --depth=0 flag to list out only the top packages and not their dependencies.
API
In your case, you want to run this within your script, so you'd need to use the API. From the docs:
npm.commands.ls(args, [silent,] callback)
In addition to printing to stdout, the data will also be passed into the callback.

Why not grab them from dependencies in package.json?
Of course, this will only give you the ones you actually saved, but you should be doing that anyway.
console.log(Object.keys(require('./package.json').dependencies));

for package in `sudo npm -g ls --depth=0 --parseable`; do
printf "${package##*/}\n";
done

As the end of 2021, there are few obvious way to do it, and a part as the only one give on the answer above this is a complete list.
The Node.js Documentation is actually pretty well explained regarding the matter, this is a collective list of the main commands.
All Commands will run the list of installed modules Locally. In order to run global level just add a -g flag at the end of the statement.
See the version of all installed npm packages, including their dependencies.
❯ npm list
>>> /Users/joe/dev/node/cowsay
└─┬ cowsay#1.3.1
├── get-stdin#5.0.1
├─┬ optimist#0.6.1
│ ├── minimist#0.0.10
│ └── wordwrap#0.0.3
├─┬ string-width#2.1.1
│ ├── is-fullwidth-code-point#2.0.0
│ └─┬ strip-ansi#4.0.0
│ └── ansi-regex#3.0.0
└── strip-eof#1.0.0
Get only your top-level packages
npm list --depth=0
Get the version of a specific package by specifying its name.
npm list <package-name>
See what's the latest available version of the package on the npm repository
npm view <package-name> version
Install an old version of an npm package using the # syntax
npm install #
npm install cowsay#1.2.0
Global package
npm install -g webpack#4.16.4
Listing all the previous versions of a package
npm view cowsay versions
[ '1.0.0',
'1.0.1',
'1.0.2',
'1.0.3',
'1.1.0',
'1.1.1',
'1.1.2',
'1.1.3',
....
]
Update all the Node.js dependencies
Install new minor or patch release
npm update
Install new minor or patch release but not update package.json
npm update --no-save
To discover new releases of the packages, this gives you the list of a few outdated packages in one repository that wasn't updated for quite a while
npm outdated
Some of those updates are major releases. Running npm update won't update the version of those. Major releases are never updated in this way because they (by definition) introduce breaking changes, and npm wants to save you trouble.
To update all packages to a new major version, install the npm-check-updates package globally:
npm install -g npm-check-updates
ncu -u
This will upgrade all the version hints in the package.json file, to dependencies and devDependencies, so npm can install the new major version
Dev Dependency
Install in development dependencies.
npm install <package-name> -D
npm install <package-name> --save-dev # same as above
Avoid installing those development dependencies in Production with
npm install --production
Uninstalling npm packages
npm uninstall <package-name>
npm uninstall -g <package-name> # globally uninstall
Uninstall a package and ** remove the reference in the package.json**
npm uninstall <package-name> -S
npm uninstall <package-name> --save # same as above
Some commands with global flag examples.
npm list -g
npm list --depth=0 -g
npm list <package-name> -g
npm view <package-name> version -g
Additional Commands
Answer by #prosti
Documentation
Find the installed version of an npm package
Install an older version of an npm package
Update all the Node.js dependencies to their latest version
Semantic Versioning using npm
Uninstalling npm packages
npm global or local packages
npm dependencies and devDependencies
The npx Node.js Package RunnerTABLE OF CONTENTS

Related

how to npm install only devDependencies with node 8.7.x?

The usual way of installing only devDependencies was to use npm install --only=dev (or --only=production if you want only dependencies).
This doesn't work anymore in 8.7. If I run this command, npm will try installing all dependencies. Or at least, it runs a /usr/bin/git ls-remote -h -t on packages that are not in devDependencies. Those packages being in private git repos, the npm install fails for me.
This didn't happen until I upgraded to 8.7.0, from 7.4.0
The npm cli documentation still shows the old way of doing it though.
Is there a new syntax for that option?
From the output of npm help install:
npm install (in package directory, no arguments):
Install the dependencies in the local node_modules folder.
In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
By default, npm install will install all modules listed as dependencies in npm help 5 package.json.
With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies.
So it seems you can install only dependencies with --production; not sure if there's a way to only install devDependencies.

how to create a node package manager

my github repository application how to Create mypkgname CLI package manager application
I have registered this package in npm, so I can install it globally:
npm install -g mypkgname-cli
mypkgname init myApp
cd myApp
npm install
npm start
just examples
npm install -g mern-cli
npm install -g #angular/cli
You may want to use some cli arg pareser like commander
or yargs
There are many other command line argument parsers.
This tutorial here explains the usage well https://developer.atlassian.com/blog/2015/11/scripting-with-node/
Important parts include
adding #!/usr/bin/env node on top of your entry point script.
in package.json, something like
"bin": {
"mycliapp": "index.js"
}
then running npm link or npm install -g on your app.

Node update a specific package

I want to update my Browser-sync without updating all my node packages. How can I achieve this? My current version of Browser-sync does not have the Browser-sync GUI :(
├─┬ browser-sync#1.9.2
│ ├── browser-sync-client#1.0.2
Most of the time you can just npm update (or pnpm update or yarn upgrade) a module to get the latest non breaking changes (respecting the semver specified in your package.json) (<-- read that last part again).
npm update browser-sync
-------
pnpm update browser-sync
-------
yarn upgrade browser-sync
Use [p]npm|yarn outdated to see which modules have newer versions
Use [p]npm update|yarn upgrade (without a package name) to update all modules
Major version upgrades:
In your case, it looks like you want the next major version (v2.x.x), which is likely to have breaking changes and you will need to update your app to accommodate those changes. You can install/save the latest 2.x.x by doing:
npm install browser-sync#2 --save-dev
-------
pnpm add browser-sync#2 --save-dev
-------
yarn add browser-sync#2 --dev
...or the latest 2.1.x by doing:
npm install browser-sync#2.1 --save-dev
-------
pnpm add browser-sync#2.1 --save-dev
-------
yarn add browser-sync#2.1 --dev
...or the latest and greatest by doing:
npm install browser-sync#latest --save-dev
-------
pnpm add browser-sync#latest --save-dev
-------
yarn add browser-sync#latest --dev
Note: the last one is no different than doing uninstall followed by install like this:
npm uninstall browser-sync --save-dev
npm install browser-sync --save-dev
-------
pnpm remove browser-sync --save-dev
pnpm add browser-sync --save-dev
-------
yarn remove browser-sync --dev
yarn add browser-sync --dev
The --save-dev part is important. This will uninstall it, remove the value from your package.json, and then reinstall the latest version and save the new value to your package.json.
Use npm outdated to see Current and Latest version of all packages.
Then npm i packageName#versionNumber to install specific version : example npm i browser-sync#2.1.0.
Or npm i packageName#latest to install latest version : example npm i browser-sync#latest.
NPM
Update Specific Package to the Latest Version:
npm update browser-sync
Update a Package By Version:
npm view browser-sync versions (view package version)
npm install browser-sync#2
Update all packages to the latest versions:
npm outdated (this checks the registry to see if any installed packages are currently outdated)
npm update --save/--save-dev (updates and saves dependencies in package.json)
Run a security audit for all the packages:
npm audit (submits a description of the dependencies configured in your project to your default registry and asks for a report of known vulnerabilities)
npm audit fix (fix vulnerabilities)
Yarn
Updates all packages to the latest version:
yarn upgrade
Updates specific package to the latest version:
yarn upgrade browser-sync
Updates specific package to specific version:
yarn upgrade browser-sync#^2
Pnpm
Updates all dependencies, adhering to ranges specified in package.json:
pnpm up (alias of pnpm update)
Updates all dependencies, ignoring ranges specified in package.json:
pnpm up --latest
Updates browser-sync to the latest version on v2:
pnpm up browser-sync#2
Updates all dependencies under the #babel scope:
pnpm up "#babel/*"
The legacy-peer-deps command can be helpful as well, especially if you're dealing with some dependency issues and whatnot.
Example:
If the package is ngx-multi-window and it's on version 0.3.1
You would run: npm install ngx-multi-window#0.3.2 --legacy-peer-deps

NPM shrinkwrap in development

What happens when you do npm install in a dev environment on a project that has both a package.json and a npm-shrinkwrap.json file?
Will it ignore the shrinkwrap and use package.json for everything or just for the dev dependencies?
Any files added to the production dependencies in package.json will be ignored if they are not in the npm-shrinkwrap.json. Vis-à-vis: running npm install with foo-package added to the production dependency list will not install foo-package.
Not so for devDependancies.
Running npm install with foo-package added to the devDependency list will install foo-package even if it is not found in the npm-shrinkwrap.json file.
Fun.
Node: v4.2.4
NPM: 2.14.12

npm install error from the terminal

I am trying to install node in my mac..
i am getting the following error...
i downloaded the node from node site and ran that package...
can you guys tell me why i am facing that errror..when i do npm install
MacBook-Pro:~ Raj$ npm install
npm ERR! install Couldn't read dependencies
npm ERR! package.json ENOENT, open '/Users/Raj/package.json'
npm ERR! package.json This is most likely not a problem with npm itself.
npm ERR! package.json npm can't find a package.json file in your current directory.
npm ERR! System Darwin 13.0.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/Raj
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! path /Users/Raj/package.json
npm ERR! code ENOPACKAGEJSON
npm ERR! errno 34
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/Raj/npm-debug.log
npm ERR! not ok code 0
Running just "npm install" will look for dependencies listed in your package.json. The error you're getting says that you don't have a package.json file set up (or you're in the wrong directory).
If you're trying to install a specific package, you should use 'npm install {package name}'. See here for more info about the command.
Otherwise, you'll need to create a package.json file for your dependencies or go to the right directory and then run 'npm install'.
I had this problem when trying to run 'npm install' in a Terminal window which had been opened before installing Node.js.
Opening a new Terminal window (i.e. bash session) worked. (Presumably this provided the correct environment variables for npm to run correctly.)
In my case it was due to a bad URL (http:// instead of git://, no .git at the end) for one of the dependencies.
You're likely not in the node directory. Try switching to the directory that you unpacked node to and try running the command there.
In case it helps anyone else - my issue was a rookie error, I had a space in the name line of my package.json and it caused the dependencies to be unreadable.
I came across this, and my issue was using an older version of node (3.X), when a newer version was required.
The error message actually suggested this as well:
...
Make sure you have the latest version of node.js and npm installed
...
So the solution may be as simple as upgrading node/npm. You can easily do this using nvm, the "Node Version Manager"
After you've installed nvm, you can install and use the latest version of node by simply running this command:
nvm install node
For example:
$ nvm install node
Downloading https://nodejs.org/dist/v8.2.1/node-v8.2.1-darwin-x64.tar.xz...
######################################################################## 100.0%
Now using node v8.2.1 (npm v5.3.0)
$ node --version
v8.2.1
In mac you might have downloaded and installed Node js in
/Users/yourusername/Downloads/nodejs-todo-master , so go here and run npm install command, no need of sudo as well., you should get output like this...
underscore#1.4.4 node_modules/underscore
ejs#0.8.8 node_modules/ejs
redis#0.8.6 node_modules/redis
jasmine-node#1.0.28 node_modules/jasmine-node
├── walkdir#0.0.7
├── coffee-script#1.8.0 (mkdirp#0.3.5)
├── requirejs#2.1.15
└── jasmine-reporters#1.0.1 (mkdirp#0.3.5)
express#3.0.6 node_modules/express
├── methods#0.0.1
├── fresh#0.1.0
├── range-parser#0.0.4
├── cookie-signature#0.0.1
├── buffer-crc32#0.1.1
├── cookie#0.0.5
├── commander#0.6.1
├── mkdirp#0.3.3
├── debug#2.1.0 (ms#0.6.2)
├── send#0.1.0 (mime#1.2.6)
└── connect#2.7.2 (pause#0.0.1, bytes#0.1.0, formidable#1.0.11, qs#0.5.1)
First download json package file from https://github.com/npm/read-package-json
and then run npm install from terminal.
This is all because you are not in the desired directory. You need to first get into the desired directory. Mine was angular-phonecat directory. So I typed in cd angular-phonecat and then npm install.
If someone is in my situation facing this error and have tried all the above solutions, like:
you are in the right directory
you have a package.json file,
the JSON is valid,
you have tried to run %temp%
you have tried " npm install -d --save"
etc.
Mine worked by doing "npm install --force"
Note: This was also recommended in the error itself, which I didn't pay attention to earlier.
Even " Yarn install" worked.
npm install -d --save worked for me. -d flag command force npm to install your dependencies and --save will save the all updated dependencies in your package.json
For me I'm on windows 10 X64...
My code npm install on cmd failed
So instead of npm i used Yarn
Just type yarn install instead of npm install
This fixed my problem.Tried for 2 days finally found the best
solution
To install yarn , on cmd enter the following code
npm install --global yarn
To check if it has installed correctly enter the following code
yarn --version
Hey if you found error and it stuck while installing then try this
Open run and type %Temp% and delete all file
Then type prefetch on run app an delete all files then try it
These Will do the Job
npm install -g yarn
yarn install
or
npm install --force
Hey if you found error and stcuk while installing packages
,getting only three files like json file ,lock file and module file using yarn then try this using yarn.
Open run and type %Temp% and delete all file
Then type prefetch on run app an delete all files
Then type on CMD npx create -react-app it will give you all packages

Categories

Resources