Learn how to set up a personalized command to execute my npm package

Recently, I developed an npm package called chat in nodejs. As of now, it is not yet available on npm. The package consists of two main files - server.js and client.js. Previously, I used to execute node server.js and node client.js separately to run these files.

Now, I am looking for a way to use custom commands like chat server and chat client to launch server.js and client.js respectively. Can anyone provide guidance on how to achieve this?

I have conducted various searches on Google in hopes of finding a solution, but unfortunately, haven't come across any useful information so far.

Your assistance in this matter would be greatly appreciated. Thank you!

Answer №1

In order to execute custom commands, it is necessary to include the bin property within the package.json file as shown below:

"bin": {
  "custom command one": "./commandOne.js",
  "custom command two": "./commandTwo.js"
}

Afterwards, run the npm link command in the project directory to establish a symbolic link between your package and the global NPM executable, enabling direct execution of the commands.

Ensure that the files referenced in the bin property begin with #!/usr/bin/env node.

If the custom commands consist of multiple words, they must be enclosed in quotes for proper execution.

The 'custom command one' will execute the commandOne.js file, while the 'custom command two' will execute the commandTwo.js file.

Alternatively, you can add the scripts property in the package.json file as illustrated below:

"scripts": {
  "custom:commandOne": "node commandOne.js",
  "custom:commandTwo": "node commandTwo.js"
}

Executing the npm run custom:commandOne command will run commandOne.js, while npm run custom:commandTwo will run commandTwo.js.

I strongly advise reviewing the NPM documentation on bin and scripts: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#bin https://docs.npmjs.com/cli/v10/configuring-npm/package-json#scripts

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

ERESOLVE encountered difficulty in resolving the dependency tree

I've encountered a lot of issues while trying to run my existing project. During installation, I keep getting the error message "ERESOLVE unable to resolve dependency tree." https://i.stack.imgur.com/Ec851.png npm ERR! code ERESOLVE npm ERR! ERESOLVE ...

Node.js encountered an error: Module "express" not found

I just created my first node.js application, but I'm having trouble finding the express library: C:\ChatServer\Server>node server.js module.js:340 throw err; ^ Error: Cannot find module 'express' at Function. ...

"Exploring the intricate world of Node module versioning

Having a node module called demo-npm-module, with various versions published on npm such as: 1.0.0 1.1.0 2.0.0 3.0.0 I encountered an issue where I needed to fix a bug in an older version like 1.1.0, leading to an update to version 1.1.1. After making an ...

What are the steps to inject the npm package called lodash as a dependency injection in AngularJS for the angular-google-maps module with the help

I have set up my angular application using npm as the package manager and Browserify to manage libraries. The specific package I am using is angular-google-maps from http://angular-ui.github.io/angular-google-maps. An error message I encountered is: Refe ...

When using Javascript, you can expect to receive a modified structure that is different from

I am dealing with an array of objects that have the following structure: const data: [ { id: 21786162, label: "cBTC 2021-06-25 Put $50,000.00", active": true, type: "put", strike_price: 5000000, date_live: "2019-11- ...

Enlist partial components in express-handlebars

I'm having trouble registering partials in my app. Despite trying various solutions from other sources, nothing seems to work for me... I have set up the express handlebars as follows: import { engine } from 'express-handlebars'; const __fi ...

Can node.js handle the workload of a magazine or news website?

I have been impressed with the speed at which Node.js can build web apps, but I am curious about its performance when it comes to a magazine or news style website. Traditional CMS platforms like Joomla, Drupal, and WordPress are known for their effective ...

Unexpected Undefined Return in Request Parameters

Hey everyone, I'm currently working on setting up a mock API server using a JSON file with some dummy data. The first route I created is functioning perfectly: const express = require("express"); const router = express.Router(); const data = requir ...

Run a Powershell command remotely in the background

I am attempting to connect to a Windows machine using SSH and run a command in the background. When I directly run the following command from the terminal after establishing an SSH connection, it works: start PowerShell.exe -ExecutionPolicy Unrestricted I ...

The wait function does not pause execution until the element is found within the DOM

Upon clicking the Next button to proceed with my test, I encountered a transition on the page that prevented me from inputting the password. To solve this issue, I implemented the wait method to pause for 1 second until the element is located. The error ...

Encountered a problem while attempting to post in Angular, receiving an error message stating "net::ERR

I recently started learning Nodejs. I've created an API on a local server using Mysql and I'm working on the frontend with Angular, while using Nodejs and Express as the backend. However, I'm facing an issue where my Angular app cannot conne ...

The error message "TypeError: Buffer.alloc is not a function when trying to access Firebase tools"

Attempting to access Firebase console from my terminal, but encountering an error that is hindering me. https://i.stack.imgur.com/2NGFF.png I also looked into this post, but since I have no experience with node.js, it was difficult for me to grasp the so ...

Restoring NPM packages within Visual Studio is always done in the System32 directory

Every time I attempt to execute the Restore Packages command by right-clicking on the package.json file, the output below is displayed: PATH=.\node_modules\.bin;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External ...

What is the best way to eliminate or substitute Unicode Characters in Node.js 16?

Currently, I have a file that is being read into a JSON Object. { "city": "Delicias", "address": "FRANCISCO DOMÍN\u0002GUEZ 9" } We are using this address to send it to the Google Maps API in order to ...

After running the `npm run build` command in a Svelte (not Svelte Kit) application, the index.html file displays as

When I run the npm run dev server, it displays the default counter app that is built. However, if I build a plain HTML, CSS, and JavaScript project using npm run build in the dist folder, then open the index.html file, it shows a blank page even though the ...

Exploring the variation between server port and websocket port in a Node.js chat system

I'm currently working on developing a multi-room chat application in node.js that uses socket.io and express. I've been having some confusion regarding the differences between the server port and the websocket port. As far as I know, the server p ...

Can someone explain the function of statements such as (function () { // code; }.call(this)); within a JavaScript module?

By utilizing the Function.prototype.call() method, it becomes possible to create a function that can be applied to various objects. I delved into the code of , which had been minified and required to be un-minified for examination. The structure of the co ...

The sequence of express routing does not transition automatically from a POST request to the next GET request

I have the following route defined: router.post('/select', function(req, res, next){ var sql = "SELECT metadocid, col1, col2, col3 FROM mdt1 where "; var metadocid = req.body.metadocid; var op1 = req.body.op1; if (metadocid !== "") ...

Issues with saving data to MongoDB using NodeJS, ExpressJS, and EJS database connection

I am in the process of developing a childcare management web application using NodeJS with ExpressJS, mongoose, and EJS. The application features a module for adding participants and another module to track each participant's daily meal count four tim ...

Switch over your operations to be compatible with Node 16 instead of Node 12 on GitHub

Despite previous claims that this issue was resolved elsewhere, it is still unresolved. I have encountered a complaint about the use of Node 12 instead of Node 16 in the following code snippet: No specific documentation outlining the disparities between N ...