Can my NPM CLI package be executed on CMD without installing globally? - javascript

I have written an NPM package which has its own CLI commands.
Let's name that package as xyz and imagine it's now avaialable on npmjs.com
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 command 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": {
........

Here'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 command, you'll need too have the user add the directory to their Path so when the user enters the command, the computer looks for the command 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.com/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!

Related

Custom NPM package - Custom command line command

I recently created an NPM package called Project_1 (which I installed in another of my Node.js projects called Project_2) using the command:
npm install --save ./path/to/Project_1
(Is there a better method to install a local package inside one other locally?)
So the packaje.json of Project_2 is as follows:
{
"name": "Project_2",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"dependencies": {
"Project_1": "file:../Project_1",
}
}
In the npm package I created (Project_1), the JSON package is as follows:
{
"name": "Project_1",
"scripts": {
"custom-serve": "http-server ./website --port 8888 -o"
}
}
When I am in the root of Project_1 I can launch from the terminal:
npm run custom-serve
And in this way I can execute my custom command.
I need to know how to call npm run custom-serve inside Project_2 (also from the command line) after installing the Project_1 package as an npm dependency in Project_2.
So, in the root of Project_2 I would like to be able to run npm run custom-serve so that the command written in the Project_1 library is triggered.
How can I do this? How should I set the JSON packages? Is there a best practice for doing this? Do I need to add special scripts in .js?
This is because I have noticed that always when installing npm packages they provide commands that can be launched from the root of the project in which they are installed.
NB: The command custom-serve is just an example. I would like to create some custom commands inside the Project_1 and i want to be able to call them inside the Project_2 after the npm install of the Project_1 package.
I have already tried to create a script inside the Project_2 like so:
"scripts": {
"custom-command": "cd ./node_modules/Project_1 && npm run custom-serve",
}
But it doesn't works.
Firstly, to answer your question if there's a better way to install a local dependency, there is via npm link.
Assuming the name in Project 1's package file is project-1, you can link it in Project 2 as follows (obviously using the paths that correspond to your setup):
cd ~/projects/project-1
npm link
cd ~/projects/project-2
npm link project-1
Secondly, if you want your package to expose a runnable command, you should configure this in the bin section of its package.json. The path should point to an executable file, so to reuse the npm script, you could use execute as follows
custom-command.js
const {exec} = require('child_process');
exec('npm run custom-serve');
And then add the bin section to the package.json of Project 1:
"bin": {
"custom-command": "./custom-command.js",
}
If you have an index file inside your Project_1, and you had Project1 as a dependency for Project2, you can just call the file and it will run the default start command:
In Project_1 package.json file:
{
"name": "Project_1",
"scripts": {
"custom-serve": "http-server ./website --port 8888 -o",
"start": "npm run custom-serve"
}
}
In Project_2 package.json file:
{
"name": "Project_2",
"scripts": {
"custom-command": "node Project_1"
}
}

Installing local Node.js project globally

I'm trying to install my custom made project globally across my system so I can call it anywhere in my computer from the cmd, but it doesn't quite work.
I tried:
npm install -g .
and more but I can't remember. This is my package.json:
...
"name": "nodejs-insta-reddit-cli",
"version": "1.0.0",
"description": "",
"main": "./build/index.js",
"bin": {
"instapostwouter": "./build/index.js"
},
...
The reason the main path is in ./build/index.js is because I'm using typescript.
So my question is: How would I install my project globally so that I can use it on my workstation in any directory?
Use npm link in the directory of local module you want to access globally.
Then you add it using npm link package_name.
OR you can publish the module (Private or Public) and use install it globally and use it.
More information on Npm Link
NOTE: if you are working with cli package then make sure you have a bin folder and entry inside the package.json file.

Run two scripts simultaneously

I created a web application in azure to host my application js node. (Azure web application)
In my project I have an api in express that is in the app.js, however in the same project I have another file which is a cronjob.
In my package.json I have the following configuration for script:
"scripts": {
"start": "node app.js"
}
When deploying through github, the api that is in the app.js works perfectly.
My question: How do I run cronjob.js simultaneously with app.js?
You can start multiple application by using "pm2" node_module.
After installing pm2 module you can start your application by using the following command in terminal.
pm2 start app.js && pm2 start cronjob.js
You may also use forever node module.
Another option to running multiple scripts simultaneously is npm-run-all.
Install with:
npm install --save npm-run-all
Then setup your "scripts" section in your package.json like so:
"scripts": {
"app": "node app.js",
"cronjob": "node cronjob.js",
"start": "npm-run-all --parallel app cronjob"
}
And start with npm start as usual.
If the only requirement is that, I think there is no need to use another tool. Simply, you can achieve this with a single ampersand &.
"scripts": {
"start": "node app.js & node cronjob.js"
}

Custom startup command for Node.js app on Azure with Babel 6

I'm having the same issue as this question which wasn't really resolved as the original questioner abandoned this route. I'm trying to run a node app on Azure using Babel6. The package.json file I'm using has the following in it:
"scripts": {
"start": "node ./node_modules/babel-cli/bin/babel-node.js server.js"
}
I've checked using the Azure console and the babel-cli node module is installed and the server.js file is in wwwroot. Despite this I get the following when I commit to Azure:
remote: Start script "./node_modules/babel-cli/bin/babel-node.js
server.js" from package.json is not found.
The npm version running on Azure is 3.10.3, node version is 6.6.0. Can anyone advise on how to get this up and running. Any help much appreciated!
It seems that to run node.js applications in ES2015 on Web Apps. We need to compile them to ES5 version, you can leverage Custom Deployment Script to achieve this.
Here is example repos on GitHub, which leverages gulp and custom deployment script to compile the node.js from ES6 to ES5 via the Azure deployment task.
Specially, this example defines several scripts in package.json for the deployment task calling:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "gulp nodemon",
"build": "gulp build"
},
And manually call the npm scripts in deploy.cmd before KuduSync task move the deployment folder to production folder.
:Deployment
echo Handling node.js deployment.
:: 0. Select node version for build
call :SelectNodeVersion
:: 1. Install build dependencies
pushd "%DEPLOYMENT_SOURCE%"
call :ExecuteCmd !NPM_CMD! install --production
IF !ERRORLEVEL! NEQ 0 goto error
popd
:: 2. Run build command
pushd "%DEPLOYMENT_SOURCE%"
call :ExecuteCmd !NPM_CMD! run-script build
IF !ERRORLEVEL! NEQ 0 goto error
popd

Not able to install "node_module"s

I am a node.js beginner..!!
I am trying to install node_modules but getting following error.
I am on Windows and using node.js v5.7.0
Also, getting the same error on Mac OS X.
First this is not an error but a warning saying you don't have a package.json file. This file could look like this for a beginning.
{
"name": "project name",
"version": "0.0.1",
"private": true,
"main": "main.js",
"start": "node main.js",
"dependencies": {
"colors": "^1.1.2"
}
}
Check this to why using a package.json file.
Edit
You can also run npm init (suggested by #Nicklas Ridewing)
You need to create a package.json file before you run npm install colors
Just run
npm init
And follow the guide to create one :)
Use npm init to setup you project package.json.
Use npm install <package-name> --save to save it to your dependencies.
Use npm install <package-name> --save-dev to save it to your development dependencies.
Those will be saved in to your package.json which will be your project main configuration file.

Categories