Change the install directory, npm with package.json - node.js

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.

Related

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

tried to npm link and run the package after but throws an error "no such file"

i did an npm link to my node package for generating password. however, after npm link and it throws these error:
npm WARN saveError ENOENT: no such file or directory, open '/Users/valeryfun/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/Users/valeryfun/package.json'
i've included bin and repository type, but i still cant run the package.
here's the package.json file:
{
"name": "password-generator",
"version": "1.0.0",
"private": true,
"description": "A password generator",
"main": "index.js",
"preferGlobal": true,
"bin": {
"password-generator": "./index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Valery",
"license": "MIT",
"dependencies": {
"chalk": "^4.1.1",
"clipboardy": "^2.3.0",
"commander": "^8.1.0"
},
"repository": {
"type": "git",
"url": "https://github.com/valeryfun/password-generator-lazy.git"
}
}
did i miss out anything such that I couldn't use the package after linking it?

how to solve it: internal/modules/cjs/loader.js:883

Hi I have an error trying to run yarn dev (node ./index.js)
any ideas to solve? maybe I need to install something?
my package.json is but I don't know why it doesn't execute
{
"name": "instaclone",
"version": "1.0.0",
"description": "The Instaclone app server",
"main": "index.js",
"author": "Cesaru",
"license": "MIT",
"scripts": {
"dev": "node ./index.js"
},
"dependencies": {
"apollo-server": "^ 2.19.0",
"dotenv": "^ 8.2.0",
"graphql": "^ 15.4.0",
"mongoose": "^ 5.11.3"
}
}
try removing node_modules folder and run yarn install again

Update Packages.json automatically with the pre-installed moduels in NODEJS

In my local machine, I worked on a node.js app, I had installed many packages before initializing the package.json.
When I initialized the Package.json using:
npm init
I got the package.json but it does not contain all the dependencies that exist in the node_modules,
Which caused me a problem of missing packages when I moved to the production server (I run npm install)
Is there a way to automatically include all of them in the dependencies part.
Here is the JSON file I have:
{
"name": "xxxx",
"version": "1.0.0",
"description": "xxxxxxx",
"main": "server.js",
"dependencies": {
"date-utils": "^1.2.21",
"mssql": "^4.0.4",
"mysql": "^2.11.1",
"seriate": "^0.9.0",
"websocket": "^1.0.23",
"winston": "^2.2.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.com/odot-web/hefner-socket.git"
},
"keywords": [
"nodejs",
"hefner",
"socket"
],
"author": "xxxx",
"license": "ISC",
"bugs": {
"url": "https://gitlab.com/odot-web/hefner-socket/issues"
},
"homepage": "https://gitlab.com/odot-web/hefner-socket#README"
}

Install node package dynamically according to parameters

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>

Resources