I have written an NPM package which has its own CLI mands.
Let's name that package as xyz
and imagine it's now avaialable on npmjs
So, let's say a user installs this package in his project by running npm install xyz
.
And now he wants to run a CLI mand provided by xyz
package on his terminal in his project.
xyz do_this
Can this be done without installing this package globally by user ? Or without any further configuration for the user ?
Here's some part of the package.json
of the xyz package.
{
"name": "xyz",
"version": "1.0.0",
"description": "Description",
"main": "index.js",
"preferGlobal": true,
"bin": "./index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
........
I have written an NPM package which has its own CLI mands.
Let's name that package as xyz
and imagine it's now avaialable on npmjs.
So, let's say a user installs this package in his project by running npm install xyz
.
And now he wants to run a CLI mand provided by xyz
package on his terminal in his project.
xyz do_this
Can this be done without installing this package globally by user ? Or without any further configuration for the user ?
Here's some part of the package.json
of the xyz package.
{
"name": "xyz",
"version": "1.0.0",
"description": "Description",
"main": "index.js",
"preferGlobal": true,
"bin": "./index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
........
Share
Improve this question
asked May 24, 2021 at 20:18
Tharindu ThisarasingheTharindu Thisarasinghe
4,0089 gold badges45 silver badges75 bronze badges
2 Answers
Reset to default 5Here's how npm works. When you install a package in the local directory, it installs the executable dependencies files inside node_modules inside the folder with package.json, but if you use --global
, it places it globally where the user has set their path.
For example, when I run npm install http-server
, my executable ends up as ./node_modules/http-server/bin/http-server
but when I install it globally, I have it as node_modules/http-server/bin
The work around for this is, if you want to just run the executable, just execute it inside like so ./node_modules/http-server/bin/http-server
. If you want it as a mand, you'll need to have the user add the directory to their Path so when the user enters the mand, the puter looks for the mand inside that folder.
Here's a guide to adding PATH directories in Linux, you'll just add the directory /pathtofolder/node_modules/http-server/bin/
or whatever your folder is. https://linuxize./post/how-to-add-directory-to-path-in-linux/
For example, if I wanted to add http-server
from my local folder to my path, I would run
export PATH="/pathtofolder/node_modules/http-server/bin/:$PATH"
Good luck! Let me know how I can help you!
The easiest way, as also given in the ment above, is to install the needed package locally like
$ node install [@<scope>/]<package>
and run the mand later on with npx from same directory
$ npx <package/mand>
We do this in our build systems to avoid global installs and their permission issues.