Retrieve the source directory when running a globally installed npm command line interface (CLI) package

I'm in the process of developing my own Command Line Interface (CLI) using NodeJS. One of the tasks for the CLI involves recursively copying folders from a source folder within the project.

After globally installing my CLI with npm install -g in the CLI project's folder, I attempted to run it in a different test folder. However, the recursive copy operation failed because it was looking for files from the source folder within the test folder instead of

/usr/local/lib/node_modules/[myCli]
.

I've experimented with various solutions involving path, require.resolve, and __filename, but they all return a path originating from the test folder where I executed the CLI.

Aside from explicitly specifying the source path as /usr/local/lib...., there must be a way to retrieve the directory of the globally executed script, right?

Answer №1

When looking for the current working directory, you can use process.cwd() here

The __filename variable provides only the path of the file where it is installed in node_modules

To locate your project files, consider using a rc file in your home directory that contains the path to your project. Learn more about run commands here

I hope this information is helpful!

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

Encountering challenges in setting up the Gulp package

{ "name": "university", "version": "2.0.0", "devDependencies": { "webpack": "latest", "babel-loader": "latest", "eslint": "latest", "sass-loader": "latest", "gulp-sourcemaps": "latest", "autoprefixer": "latest", "postcss-l ...

Is there a reason why the layout.jade isn't functioning properly?

I have encountered an issue with my configure file: The layout.jade is not working properly, even though the jade itself is functioning correctly. I have verified this using Chrome and confirmed that the layout HTML is not being loaded into the page. modu ...

Error encountered in Ubuntu while attempting to run a Python script within a Node.js/Express application: spawn EACCES

Recently, I set up a node.js server to run a python script using the python-shell . However, after migrating from Windows to Ubuntu, an EACCES error has been persistently popping up. Despite my attempts to adjust permissions and troubleshoot, I haven' ...

Updating a Mongoose document will only modify a single value

Trying to update multiple values in MongoDB using Mongoose. Specifically, I want to update all values with a certain field that is smaller than a specified value. Here's what I have attempted: var conditions = {Number: {$lt : 6000}}; var upda ...

Node/Express API returning empty body when being accessed through fetch or axios requests

Currently working on integrating an API call in a React app using Node/Express. No matter where I place the fetch/axios call, the parsed body always shows up as undefined in my controller. Yesterday, I was experimenting with fetch but decided to switch to ...

Is it feasible to add on to an existing object in the database? (Using Node.js and Mongoose

After saving an object to the database using the following code: var newObject = new PObject({ value : 'before', id : 123456 }); newObject.save(function(err) { if (err) ...

Having trouble downloading files in React and Node.js

I need to retrieve a specific file from the uploads folder and download it into my download folder. Both folders are located in the same directory, and the file's name is BasicUserFile-id.jpg. On my second attempt, I tried hardcoding the file name bu ...

Guide to extracting the values associated with a specific key across all elements within an array of objects

My goal is to retrieve the values from the products collection by accessing cart.item for each index in order to obtain the current price of the product. const CartSchema = mongoose.Schema({ userId: { type: mongoose.Schema.Types.ObjectId, ...

Avoiding unlimited re-renders when using useEffect() in React - Tips and Strategies

As a new developer, I recently built a chat application using socket io. In my code, I have the useEffect hook set to only change when the socket changes. However, I also have setMessage within the body of useEffect(), with socket as a dependency. Unfortun ...

I noticed that my node.js application is intermittently throwing an Unhandled 'error' event when processing write requests, shortly after I configured it to run behind Nginx

Having been successfully running node.js(0.8.20 and 0.9.10) on Windows Server 2012 for weeks without any issues, I recently added Nginx(1.2.6) to the mix. However, after configuring Nginx as follows: #user nobody; worker_processes 1; #error_log logs/e ...

"Enhance your command line experience by utilizing CLI tools while running bash on Windows with the

After setting up NPM and Git for Windows, along with several global NPM packages accessible from the command line, I wonder: if I use the bash shell in the Windows Subsystem for Linux, will I need to reinstall all these tools within the Linux subsystem a ...

Express is throwing a TypeError because it is unable to access the property 'app', which is undefined

On my nodejs server running the express framework, I have been encountering a random error when making requests. The error occurs unpredictably, usually appearing on the first request and not on subsequent ones. It's challenging for me to identify the ...

Error message from sails.js: `req.target` is not defined

Experiencing a problem where req.target sometimes returns undefined, causing issues with other functionalities dependent on req.target. Seeking assistance to resolve this issue. Appreciate any help! ...

Is there a way for me to view the output of my TypeScript code in an HTML document?

This is my HTML *all the code has been modified <div class="testCenter"> <h1>{{changed()}}</h1> </div> This is my .ts code I am unsure about the functionality of the changed() function import { Component, OnInit } f ...

What can I do to troubleshoot the "npm run build" command not being found error?

Within my script, this section is included: "scripts": { "start": "webpack", "compile": "tsc -p tsconfig.sequences.json", "build": "npm-run-all -p compile start", However, I ...

Utilize Relative Paths when working with node.js and express

Can someone help me troubleshoot an issue with running my node server on localhost? Currently, I am only able to run it using a static path: app.get("/", function(req, res){ res.sendFile("/Users/name/Documents/_privat/dungeon/index.html"); }); Obviou ...

My React setup is causing some problems that I need to address

React is not my strong suit, but I need to test a React application. The issue arises when attempting to run the server using gulp nodemon, resulting in numerous errors. It seems that the application is built on an outdated version of React and some libra ...

When using React.JS / Next.JS with @material-ui/core/styles, you may encounter the `ERR_UNSUPPORTED_DIR_IMPORT` error

When importing { makeStyles } from '@material-ui/core/styles', an error is encountered:</p> <pre><code>- information Collection page data ..Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/usr/src/sellerportal/node_mo ...

Utilizing AWS Websockets with lambda triggers to bypass incoming messages and instead resend the most recent message received

I am facing an issue when invoking a lambda that sends data to clients through the websocket API. Instead of sending the actual message or payload, it only sends the last received message. For example: Lambda 1 triggers Lambda 2 with the payload "test1" ...

Incorporating DefinitelyTyped files into an Angular 2 project: A step-by-step guide

I am currently developing an application using angular 2 and node.js. My current task involves installing typings for the project. In the past, when starting the server and activating the TypeScript compiler, I would encounter a log with various errors rel ...