How to install all packages from a scope - npm

Is it possible to install all packages from a given scope for example:
npm install #myscope/* --save

I don't think it's possible with npm itself, but by combining your swiss army knife of unix tools – or windows tools.
I'm on Windows and using cmder to get some level of Unix tools available:
Obtain a table with all packages in the scope, columns will be separated by tabs.
Use awk to only output the first column of that table, it’s the actual package name, including the scope, like #org/package
Use that output with npm install, to actually install these packages
npm install $(npm search #org --parseable | awk '{print $1}')

Related

With "npm list" - is there a way to use wildcard / regex?

I couldn't find a way to do e.g.:
npm list -g *json*
(Been trying npm list -g | sls json via PowerShell, but PowerShell seems to have difficulties with it as it hags for few min. after that command...)
Unfortunately there is no parameter on npm ls that enable that natively which would be a great addition.
The only solution I found so far that may help you is to use grep.
i.e.:
npm list -g | grep json
That will show only lines matching whatever you pass to grep, but that won't show the structure of dependencies correctly and then does not help to find who depends on a specific instance of that dependency, but is a start.

Is there any difference between installing global packages with Yarn or NPM?

Does it matter whether you install a global package with yarn global add PACKAGE vs npm install -g PACKAGE ?
Is there any difference at all, like where files are installed?
If yes, what is it?
So yes, you are right it is different. For npm it is something like below
/Users/tarunlalwani/.nvm/versions/node/v9.2.0/lib if you are using nvm
You can get this path using
$ npm config get prefix
/Users/tarunlalwani/.nvm/versions/node/v9.2.0
Where does npm install packages?
While yarn uses other paths
Windows: %LOCALAPPDATA%/Yarn/config/global
OSX and Linux non-root: ~/.config/yarn/global
Linux if logged in as root: /usr/local/share/.config/yarn/global
How to display yarn globally installed packages?
See this thread as well
https://github.com/yarnpkg/yarn/issues/2049
This is the document about Yarn global
yarn global is a prefix used for a number of commands like add, bin,
list and remove. They behave identically to their normal versions
except that they use a global directory to store packages. The global
command makes executables available to use on your operating system
and this is the document about npm install global mode
In global mode (ie, with -g or --global appended to the command), it
installs the current package context (ie, the current working
directory) as a global package.
I think there is no difference between them. Install a package as a global useful for developer tooling that is not part of any individual project but instead is used for local commands

replicate `npm pack` and allow for streaming to stdout

The npm pack command only allows you to write out a file to the current working directory.
I am looking for a way to do this:
npm pack > $myfile
So I am considering writing my own version of npm pack - but I dont quite know how it's implemented.
Does anyone know if npm pack as its currently implemented bundles node_modules? is there some quick and dirty way to replicate its functionality but write output to stdout?
I assume npm pack is based on the tar command and that the tar command allows you to write to stdout...
You can use yarn instead of rewriting npm
yarn pack -f some/other/path.tgz
or if you really need to redirect:
yarn pack -f /dev/stdout > some/other/path.tgz

Check if npm package is installed in package.json through terminal

I am trying to find a way to check if a particular package is installed in my project through terminal. Is there a command for that? Something like npm check redux.
you can check easily for that.
this will describe all the package installed globally
npm list -g --depth=0
this will describe all the package installed locally on your project.
npm list --depth=0
if you want to check for a particular module is installed or not.
Please use the following command in project folder.
if installed, will display package name and version installed.
if not installed, then will not display anything.
npm list --depth=0 | grep <module_name>
for more detail information please see this link. Click here for more info of your question
--depth=0 is necessary so that your terminal isn't flooded with package dependencies. if you are not use this option, you will see the all the dependencies tree.
If you are searching for specific package installed in your project, you can use one of the commands below based on what kind of terminal you use. If you are using Unix shell based terminal, you can use:
npm list --depth=0 | grep <module_name>
or if you're using windows terminal or power shell, you can use:
npm list --depth=0 | findstr <module_name>
findstr is a similar command like grep in unix shell.

How can I find out the version of an installed grunt module?

Just wondering what is the command to verify the currently installed version of any grunt module already installed using command line. For example
grunt-compass -v
Or
grunt-compass --version
do not work.
Use
npm list --depth=0
You can also use grep to look for a specific package
npm list --depth=0 | grep grunt-contrib-compass
There is an npm ls alias, for short.
Perhaps you can try this, it worked for me.
grunt -version
With Python you can do something like this, in the root of your project, remembering to substitute grunt-contrib-compass for any other package installed with npm.
cat node_modules/grunt-contrib-compass/package.json | python -c "import json, sys; print json.load(sys.stdin)['version']"
This is not my code, I've adapted it from here - Parsing Json data columnwise in shell - but I've tested it and it works. :-)
If you'd rather a node/grunt solution, you can have a look at my answer here. It's based on the project's package.json, but you could adapt that to use one in the node_modules directory.
Edit: After reading Nico's answer, you could transform that output with sed to print just the version number, like so:
npm list --depth=0 | grep grunt-contrib-compass | sed "s/[^0-9\.]//g"