What is the process for modifying the output format of npm ls?

My client is requesting the dependencies of our projects, including the library name and github/NPM URL. Currently, I am using 'npm ll' command to retrieve this information for each project, but the output is in a tree structure. Is there a way to convert this output into a table format?

I have searched through the documentation of 'npm ll' command, but I couldn't find any information on changing the output to a table.

Answer №1

If everything else fails, you always have the option to extract the necessary information using jq:

npm list --json | jq -r 'recurse(.dependencies[]) | [.name, .version, .repository.url?] | @csv'

An example of the result:

"express","4.16.4","git+https://github.com/expressjs/express.git"
"accepts","1.3.5","git+https://github.com/jshttp/accepts.git"
"mime-types","2.1.22","git+https://github.com/jshttp/mime-types.git"
"mime-db","1.38.0","git+https://github.com/jshttp/mime-db.git"
"negotiator","0.6.1","git+https://github.com/jshttp/negotiator.git"

Answer №2

This script provides a solution that may not directly answer the question, but it does offer a way to manipulate the output for better readability. Take a look at the following bash script:

#!/bin/bash
# list packages and remove tree structure prefix + "deduped" suffix (keep only module@version)
data=($(npm ls | sed 's/^[┬├│─└ ]*//g' | sed 's/deduped$//g'))
for module in ${data[*]}
do
  # split on @ only when it separates module and version
  # (not the @ prefix on @username/module@version)
  split=($(echo $module | sed -r 's/^(.+)@/\1 /'))
  # an example display
  printf "%-30s%10s https://www.npmjs.org/packages/%s\n" ${split[0]} ${split[1]} ${split[0]}
done

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize the value of res.locals.someVariable in an hbs (handlebars template)

I am facing an issue while trying to pass my session variables to handlebars templates. Currently, I have implemented the following code snippet in my app.configure function: app.use(function(req, res, next){ res.locals.session = req.session; ...

Exploring the differences between async and sync deserialization in Passport.js

In the process of adding a payment feature to my app, I encountered an issue related to the requirement of AccountId and Email by my service provider. The behavior I observed while using Passport.js for authentication is quite perplexing. Despite following ...

Be attentive for the beforeExit or SIGTERM signals in Node.js to initiate cleanup tasks

In my node.js application, I implemented a feature that listens for the beforeExit event and executes a cleanup callback to close MySQL connections before exiting. But, I've come across discussions suggesting listening for the SIGTERM event instead f ...

Utilizing Node and Express to promptly respond to the user before resuming the program's

I am accustomed to receiving a user's request, handling it, and providing the outcome in response. However, I am faced with an API endpoint that requires about 10 tasks to be completed across various databases, logging, emailing, etc. All of these ta ...

Exploring the differences between client-side and server-side data manipulation

Recently, I discovered how to access local variables from Express in client-side JavaScript: Check out this helpful thread on StackOverflow However, this discovery has brought up a new challenge for me... Now I am unsure about which data manipulation tas ...

What is the best way to relocate all matching lines of x to the beginning of the file?

How can I rearrange all the lines of code that start with or include "@use" to the top of the file while alphabetizing and removing duplicates? .css-class { @use 'text-size' as *; // How to move all lines starting with @use to the top of the fi ...

Guide on sending terminal output as JSON data to a Node API?

I am currently developing a Node API that can execute commands on the Terminal. For example, when I navigate to: http://localhost:3000/runLS, the command ls -la is executed in my working directory and the output is returned as JSON to the API. I have succe ...

Executing Node.js code asynchronously using promises in a sequential manner

Currently, I am in the process of developing a Firebase function using node.js. In the code snippet below, the "pushNotifications" function is triggered whenever a new entry is added to the messagesDB. Within the messagesDB, I extract the email from the ...

Alter the HTML file permanently through an AJAX request made to the Node.js server

Is there a way to create a script that will alter a static HTML file indefinitely following an ajax request to the node.js server? I would greatly appreciate any examples you can provide :) ...

Retrieve the package.json file for a specific package by making an HTTP request to public repositories

I’ve been searching online but haven’t found a satisfying answer to my question yet. My main objective is to generate a dependency tree for a particular npmjs library of a specific version, like retrieving the dependency tree for the angular library v ...

Utilizing Node.js, delete the author from the database and then use a GET request to display all

Exploring node and express for the first time. I've been working on an example that utilizes GET and POST methods, but now I want to implement DELETE function to delete a book based on its title. Additionally, I need to introduce another GET method to ...

Sequelize's findOne method has returned an instance, but the row data is stored in the dataValues property. However, the direct

Background: I am in the process of testing a basic model going into and coming out of the database. This is not an actual test, but rather a prelude to some integration tests. The tools being used include Sequelize and findOne. The issue: When directly ac ...

Is Jade monitoring *.jade files?

Though I am not sure of the internal workings of Jade, my best guess is that it compiles each template file once and then employs a compiled and cached version for subsequent HTTP requests. One intriguing observation I have made while running my Express a ...

Error encountered when attempting to create a new Vue project using Vue CLI (npm issue)

I'm having trouble creating a new Vue CLI project. It was working fine before, but now it's not working... Vue CLI v4.2.3 ✨ Creating project in /Web develop/Vue Projects/new. ...

Using NextJS's API routes to implement Spotify's authorization flow results in a CORS error

I am currently in the process of setting up the login flow within NextJS by referring to the guidelines provided in the Spotify SDK API Tutorial. This involves utilizing NextJS's api routes. To handle this, I've created two handlers: api/login.t ...

Is it possible to streamline the use of next() in Express middleware functions?

I have devised my route in a way that I'm not completely satisfied with. I would prefer to have separate middleware for each hasValid, but I am uncertain of how that could be implemented without interrupting the execution flow. const secretSender = a ...

Deciding Between Promises and Callbacks in Node.js

As I update my older Node.js code, I am also creating new modules to integrate with the existing code. One challenge I face is transitioning from using callbacks to utilizing ES6 promises more frequently. This has created a mixture of functions that return ...

Tips for successfully navigating and utilizing function values

I have developed models named (user.js). module.exports.show_deatils = function(req,res,callback){ var resultArray=[]; mongo.connect(url,function(err,db){ assert.equal(null,err); var cursor=db.collection('users&apos ...

Challenges arising with the express search feature

Currently, I am in the process of developing an API for a to-do application. I have successfully implemented the four basic functions required, but I am facing some challenges with integrating a search function. Initially, the search function worked as exp ...

Transferring streaming data from Node.js to an ElasticSearch database

Currently, my Node.js script is extracting data from a large USPTO Patent XML file (approximately 100mb) to create a patentGrant object. This object includes details such as publication number, country, date, and type of patent. I am working on storing a ...