npm install package global and link in one cmd - npm

I need to do npm install and link both in single cmd
So I am using
npm install -g NamPkg && npm link NamPkg
Is there any shorthand for this where I can mention NamPkg only once like :
npm install -g && npm link NamPkg
OR
npm install -g && link NamPkg
Bcoz I need to use this in Package.json
and when the list grows more mentioning the same package names twice may become lengthy
"scripts": {
"preinstall": "npm install -g NamPkg && npm link NamPkg"

Is there any shorthand where I can mention NamPkg only once
AFAIK there is no way of doing this either via npm scripts or bash.
I don't know if it fits your use case, but using a variable would save a little typing (in that you would only have to declare the package name once and could copy/paste the rest of the command).
"scripts": {
"preinstall": "pkg='whatever'; npm i $pkg && npm link $pkg"
},

Related

How to install lodash.filter using npm?

I need to install lodash.filter to my Expo (React Native) project.
How should I do it using npm?
1.
npm i -g npm
npm i --save lodash.filter
npm i --save lodash.filter
I do not understand what npm i -g npm (it's updating something, but I think it will destroy my project).
$ npm i -g npm
$ npm i --save lodash
you can use second command.
npm i is the alias for the npm install and -g means gloabal
The -g or --global argument will cause npm to install the package globally rather than locally
npm install (in package directory, no arguments):
Install the dependencies in the local node_modules folder.
Just follow the command that are written in documentation it will be fine
The --save option instructed NPM to include the package inside of the dependencies section of your package.json automatically, thus saving you an additional step.

npm install -g -D not updating dev dependencies in package.json

I've tried opening and closing the open package.json editor, and restarting VS Code, and running and re-running the npm install command at least 8 times but to no avail.
For some reason, the command below does not update my dev dependencies in my package.json file.
$ npm install -g -D webpack
I know I can manually add the dependency but just saying and this is driving me nuts.
Your are passing the option -g which makes npm install the package globally so you can execute the binary directly, remove that flag to install the package locally.
Can you try: $ npm install --save-dev webpack
This is the recommended way to install webpack from npmjs

What is the difference between NPM -g (global) install and NPM --save

What is the difference between npm -g(global) install and npm --save?
First gulp install -g and --save first, then for other projects:
npm i gulp --save-dev Can I just use this command?
I don't know the basic difference between them?
npm -g will install packages globally (to npm cached folder), normally in AppData\Roaming\npm\node_modules if you're using Windows, while npm --save or --save-dev will install package directly to your node_modules directory in your project and add package to your packages.json for later purpose.

npm install - how to trigger npm task as a post-install hook?

After npm install is run I would like to run
npm run jspm install
I have package.json
"scripts": {
"postinstall" : "npm run jspm install",
"jspm": "jspm"
},
This throws an error since npm run jspm install gets passed to node rather than npm. What's the correct way to do this?
There doesn't seem to be a reason to reference the jspm script. You can refer to jspm directly in the postinstall script:
"postinstall" : "jspm install",

Difficulty solving "sh: <npm package name> command not found" with npm run <npm package name>

I've been adding npm modules to my project for the first time (jshint, optimg, jpgo). I notice that some projects, when I do npm run [name], give a result of "sh: [name]: command not found."
I can't figure out why those don't work, but the other npm installs do. All these are installed locally; I can see the install by looking in the /node_modules folder in my project root and verify them with npm ls.
The latest package that gets this error is html-minify. My package.json looks like this (and validates at http://jsonlint.com/):
{
"name": "npmTest",
"devDependencies": {
"jshint": "latest",
"optimg": "latest",
"jpgo": "latest",
"ycssmin": "latest",
"html-minify": "latest"
},
"scripts": {
"lint": "jshint **.js",
"png": "optimg",
"jpg": "jpgo",
"css": "ycssmin **.css",
"html": "html-minify"
}
}
I tried "html-minify **.html" with the same resulting "not found" error.
Why would I get "sh: [npm package name]: command not found"? I've read the other threads, and because the other modules work, I doubt that I need to add anything to my PATH, or start a server, or install globally.
Fuller error message (for html5-lint):
$ npm run html
> npmTest# html /Users/Steve/Documents/APPS/Test_Apps/npmTest
> html5-lint
sh: html5-lint: command not found
npm ERR! npmTest# html: `html5-lint`
npm ERR! Exit status 127
npm ERR!
npm ERR! Failed at the npmTest# html script.
npm ERR! This is most likely a problem with the npmTest package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! html5-lint
npm ERR! You can get their info via:
npm ERR! npm owner ls npmTest
npm ERR! There is likely additional logging output above.
npm ERR! System Darwin 14.1.0
npm ERR! command "node" "/usr/local/bin/npm" "run" "html"
npm ERR! cwd /Users/Steve/Documents/APPS/Test_Apps/npmTest
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.24
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/Steve/Documents/APPS/Test_Apps/npmTest/npm-debug.log
npm ERR! not ok code 0
so short answer to run npm package commands locally
npm i install cowsay -d
then use
npx cowsay I am working locally
output should be
______
< I am working locally >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
on the other hand installing npm packages globaly on linux has problems with addministrator and node-module location
so best practice to get global packges to work normally is to install NVM first
sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
then once nvm is installed install from nvm node
nvm install node
or a specific version
nvm install 12.18.3
now installing global packages and running them is simple
npm install -g cowsay
cowsay I am working globaly
will output
______
< I am working >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
now global packages are working fine and local packages can be run from the directory of the project using npx.
For the command not found issue you're seeing with html-minifier it could have to do with not globally installing the npm package (with a -g) or calling the command by the wrong name (I believe you're calling the package name html-minify when the package is defined as html-minifier)
Would suggest installing the command with a -g:
npm install html-minifier -g
and then running the html-minifier command:
html-minifier --help (to test installation)
html-minifier --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true
Also for the scripts issue you're observing (where running npm run html is returning command not found)- here is a sample package.json. I ran npm run html (after globally installing html-minifier using the step above) and that runs html-minifier --help:
{
"name": "stackoverflow",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"html": "html-minifier --help"
},
"author": "",
"license": "ISC",
"dependencies": {
"html-minifier": "^4.0.0"
}
}
Github link: https://github.com/kangax/html-minifier
Npm link: https://www.npmjs.com/package/html-minifier
Installation and usage steps provided in the README:
Installation Instructions
From NPM for use as a command line app:
npm install html-minifier -g
From NPM for programmatic use:
npm install html-minifier
From Git:
git clone git://github.com/kangax/html-minifier.git
cd html-minifier
npm link .
Usage
Note that almost all options are disabled by default. For command line usage please see html-minifier --help for a list of available options. Experiment and find what works best for you and your project.
Sample command line: html-minifier --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true
Node.js
var minify = require('html-minifier').minify;
var result = minify('<p title="blah" id="moo">foo</p>', {
removeAttributeQuotes: true
});
result; // '<p title=blah id=moo>foo</p>'