Install node package dynamically according to parameters - node.js

I need to update the node.js package.json dynamically for example
if I run npm start with param A it will install
for example if I run npm start A it will start
{
"name": "simplenodeapp",
"main": "app.js",
"scripts": {
"start": "node app.js" <some param",
},
"license": "ISC",
"dependencies": {
"express":"*"
},
}
And if I run npm start B
{
"name": "simplenodeapp",
"main": "app.js",
"scripts": {
"start": "node app.js" <some param>",
},
"license": "ISC",
"dependencies": {
"HAproxy":"*"
},
}
is it possible ? I need to do it programmatically...

UPDATE:
I hope I finally understand the question being asked.
NPM allows you to define config objects and pass in dynamic arguments. These can be used separately or combined quite powerfully.
In the comments below, the OP asked how to run node with two different file names. This can be accomplished in two different ways.
Option 1:
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"author": "Skyler Hair",
"license": "MIT",
"config": {
"A": "index.js",
"B": "app.js"
},
"scripts": {
"start:A": "node $npm_package_config_A",
"start:B": "node $npm_package_config_B"
}
}
npm run start:A OR npm run start:B
Option 2:
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"author": "Skyler Hair",
"license": "MIT",
"scripts": {
"start": "node"
}
}
npm run start -- index.js OR npm run start -- app.js
ORIGINAL ANSWER:
You can create a script that runs npm install and accepts the dependencies as arguments. For example,
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"author": "Skyler Hair",
"scripts": {
"install": "npm install"
}
}
can be run using
npm run install -- express <other dependency> <another dep>

Related

error Couldn't find the binary nodemon index.js error in node.js

I am a beginner in node-js and I have been learning it using this this video. I have installed the nodemon package and referenced it in package.json. the issue is when I try to start the app in the index.js I get this
.this is my package.json
{
"name": "hotelmgtandrsrvreactandnodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"nodemon": "^2.0.20"
}
}

When typing yarn start in the terminal an error occurs stating /bin/sh: nodemon: command not found Command failed with exit code 127 comes up

My json package is below
{
"name": "node-rest-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.3",
"mongoose": "^6.2.3"
}
}
I have exhausted all solutions I have found as most of them point to me having start in my json package which I already do so,
Because nodemon module is not installed
either install manually by below command
npm i nodemon
or add nodemon in pakage.json
{
"name": "node-rest-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.3",
"mongoose": "^6.2.3",
}
"devDependencies": {
"nodemon" : "^2.0.15"
}
}
and after that npm install

Getting error: "npm ERR! missing script: start", yet start script is there

{
"name": "oculus_scraper_node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cheerio": "^1.0.0-rc.5",
"nodemailer": "^6.4.17",
"puppeteer": "^5.5.0"
}
}
When using heroku and checking if it deployed properly, I keep getting an error reading "npm ERR! missing script: start", but i definitely have the start script in my program, as you can see. I've looked through a couple other posts but nothing has worked. Any ideas?

framework7: npm ERR! missing script: serve on package.json

I added serve keyword inside package.json with localhost:8080 as it's value but it wont work. I tried using localhost ip address instead of "localhost" still got error.
On my package.json
{
"name": "package-lock",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"framework7": "^3.0.1"
},
"devDependencies": {},
"scripts": {
"serve":"localhost:8080",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

Change the install directory, npm with package.json

I have a package.json that looks something like this:
{
"name": "test",
"version": "0.0.1",
"description": "test test",
"main": "index.js",
"scripts": {
"test": "echo \"No test specified\""
} ,
"devDependencies": {
"gulp": "^3.9.1"
},
"dependencies": {
"bootstrap": "^4.0.0-alpha.5"
}
}
Is it possible to install bootstrap in another directory than "node_modues". If so, how to I write that in the "package.json" file
You can use this code in package.json:
{
"scripts": {
"preinstall": "npm install bootstrap --prefix ./another/"
}
}
your module will be installed in ./another/node_module
NOTE: So you can use another options in this scripts for installing modules as you want.

Resources