How do I see what requires a package using yarn? - javascript

My node app has a dependency that depends on node-webcrypto-ossl.
I'd like to run something like:
yarn what-depends node-webcrypto-ossl
To see the dependency chain, eg:
A
B
node-webcrypto-ossl
However I can't find anything to do this in the yarn CLI docs.
How do I see what requires a package using yarn?

yarn added the yarn why command specifically to get information on why a package is imported.
For example:
yarn why node-webcrypto-ossl

Related

Does npm/yarn install devDependencies by default?

I'm working on private library of react components. I've already setup the whole workflow etc. but I'm not sure about one thing.
In my library I have some packages listed as peerDependencies which have to be peer ones (for example react, cuz only one instance of react can be installed at once, otherwise everything breaks). But if I'd like to setup some tests in my library I need react installed there as devDependency.
So I have to install react as peer and dev in library. And what happens when I publish this package to npm register? devDependencies are excluded here?
Thanks!
If you need it in production, add it to peerDependencies. Otherwise, leave it in devDependencies. When someone installs your library, dev dependencies won't be installed, and peer dependencies should already be installed.
If you create a node package, and you have some dev dependencies, npm wont count them for the publish. That's why it's called devDependencies. It's not under the production code.
I know you have to specify when installing an npm package --save-dev so it saves dependencies onto your json file.

How to avoid install of packages in monorepo using Yarn

I built a monorepo using Lerna and Yarn workspaces.
Everything works fine but everytime I install a new dependency on a package (let's call him A) using:
yarn add <package_name>
Yarn adds it and then triggers the install script of all the packages in the monorepo, even the ones that A doesn't rely on.
It there anyway to avoid this? It takes a few moment to install them for no reason at all.
Try adding to the specific workspace with:-
yarn workspace <workspace_name> add <package_name>
For some docs check here
Using scope add the package to the particular module.
lerna add some_package_1 --scope=some_module_x
More: https://github.com/lerna/lerna/tree/master/commands/add#readme
You can try Yarn 2 with nodeLinker: node-modules in .yarnrc.yml. Yarn 2 guarantees to trigger rebuild only on packages that have their dependencies changed, this is something that was not guaranteed by Yarn 1. However there will still be a very rare case when seemingly unrelated packages be rebuilt if they are hoisted differently after adding new package, but this will happen very rarely.

Yarn global command not working

I'm working with Yarn v0.16.1. If I understand correctly (according to the documentation), yarn global add <package> should be the equivalent of npm install -g <package>. However, when I run the example in the docs (with create-react-app), the command runs successfully but create-react-app is then not available from the command line. Here's the output:
$ yarn global add create-react-app
$ yarn global v0.16.1
[1/4] πŸ” Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] πŸ”— Linking dependencies...
[4/4] πŸ“ƒ Building fresh packages...
success Installed create-react-app#0.6.0 with binaries:
- create-react-app
✨ Done in 3.22s.
$ create-react-app --help
-bash: create-react-app: command not found
Doing a global install with npm has the expected result, and everything works. What am I missing with yarn?
You should add export PATH="$PATH:$(yarn global bin)" to your ~/.bash_profile or whatever you use. It would solve the issue.
Depending on how you installed it, Yarn's global folder varies for some reason. You can follow this issue here.
Update Dec 2018
Just updating the path didn't work for me. I had to also set the yarn prefix.
Ubuntu 18.04.1 LTS (bionic)
yarn 1.12.3
npm v3.5.2
node v8.10.0
zsh 5.4.2
Steps
Confirm your global bin path
yarn global bin
I got: /home/username/.yarn/bin
set yarn prefix:
make sure your yarn prefix is the parent directory of your bin directory. You can confirm by running
yarn config get prefix
when I ran this, my prefix was empty, so I set it:
yarn config set prefix ~/.yarn
add the following to ~/.bash_profile or ~/.bashrc
export PATH="$PATH:`yarn global bin`"
for zsh users, be sure to add this line to ~/.zshrc
restart your shell or start a new one
bash -l or zsh
In my case yarn global bin wasn't working.
Try a brew reinstall yarn if that's how you installed yarn
πŸ‘πŸ»
Ran into this issue but on a Windows system. All I had to do was add the yarn global bin to the PATH variable.
setx path "%path%;c:\users\YOURUSERNAME\appdata\local\yarn\bin"
Remember to change YOURUSERNAME to your account username.
You can check where your global bin is by running
yarn global bin
And please don't forget to restart the CMD window that you're working on.
Happy coding!
! WARNING !
When executing the command that %path%; in front of your yarn directory is very important. If you don't type it you will definitely replace all your system environment variables.
Option 2
Safer option would be would be to just go to System Properties > Advanced > Environment Variables > select PATH the click Edit > then append and not replace with ;c:\users\YOURUSERNAME\appdata\local\yarn\bin
For macOS, you just need one step,
export PATH=~/.yarn/bin:$PATH
To reinstall run below.
brew install -g yarn
// Note:(updating homebrew) for Mac users.
brew reinstall yarn
// (if yarn is still not found)
Here's the process I used on Mac OSX El Capitan.
Try to install yarn (I used the Curl command) if you have it already. This will bring up if its already installed and will give you the current directory where it exists.
Like this:
curl -o- -L https://yarnpkg.com/install.sh | bash
It will display something like this:
Installing Yarn!
> /Users/{Your Username}/.yarn already exists, possibly from a past Yarn install.
> Remove it (rm -rf /Users/{Your Username}/.yarn) and run this script again.
Do not remove it. Move on to step 2.
Copy the directory listed above. Open your profile. I'm using zsh. So mine was ~/.zshrc. You can find yours and more info here.
Copy the following (replacing your directory and username details as necessary, the one you got from the installation error message).
alias yarn="/Users/{Your Username}/.yarn/bin/yarn"
Try to run yarn version to check if its working. If it is, you should see a version number displayed in your terminal.
That's it, and what worked for me.
AndrewD gave a great answer, but for those on Windows it's a bit different, especially step 3.
Please do not use the command setx as #Thapedict said.
It will remove all your previous paths you had and replace it with only the given directory.
If you want to use setx, I think there might be a flag to make it append the existing path variables, but I don't know which one. Using only setx will not append to it.
I would just do System Properties > Advanced > Environment Variables > Edit Path > New > c:\users\YOURUSERNAME\appdata\local\yarn\bin
to make sure you don't remove your previous variables.
export PATH="$PATH:$(yarn global bin)"
On Ubuntu, I fixed by installing the package like this:
sudo yarn global add <package> --prefix /usr/local
When switching from bash to zsh, I got the same error, it worked for me:
in your .zshrc
# NVM Stuff
export NVM_DIR="$HOME/.nvm"
. "$(brew --prefix nvm)/nvm.sh"
# YVM Stuff
export YVM_DIR="$HOME/.yvm"
. "$(brew --prefix yvm)/yvm.sh"
As I understand it, my mistake arose due to the lack of access to version managers
I faced a similar issue on Windows after installing Yarn then Vue Cli.
Packages were installed but not accessible.
The problem was that the directory where yarn packages are installed is not in PATH.
The default installation directory for Yarn packages in my case was
C:\Users\USERNAME\AppData\Local\Yarn
So make sure to add this directory to path.
Putting this answer so that hopefully it can come in google and help others.
expo command not found after using
yarn global add expo-cli don't work in ubuntu 20.04
It's no longer necessary to modify your path for yarn
Newer versions of yarn include yarn create and yarn exec
In the case of create-react-app you can run yarn create react-app see this blog post https://classic.yarnpkg.com/blog/2017/05/12/introducing-yarn/
To run a non-creating package like serve, you can still run it without modifying your path! After installing it with yarn global add serve try this: yarn exec serve

Pointing package.json to a specific React commit installs react-tools (not react)

When I add this line to my package.json:
"react": "git://github.com/facebook/react.git#08e4420019f74b7c93e64f59c443970359102530"
...and then run npm install, I find node_modules/react-tools installed when I expect to see node_modules/react.
What am I doing wrong here?
The code at git://github.com/facebook/react.git is not the same code that gets installed when you npm install react. Instead, the code contains a series of build steps that are used to build the npm package. As far as I know, there is not a way to easily use a specific SHA of the React repo as an npm package; you would need to clone the repo, build the project, and copy it somewhere you can require it.

Node NPM - install versus install -g

I am a node newbie and am somewhat confused with the whole "install" thing.
What is the difference between install, and install -g?
Can something installed with install -g be accessed anywhere, or does this make it available to the Node server but not your application? Is there any reason to use one, and not the other?
Cheers
From the node.js blog:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
So for example, lets say you wanted to install the Grunt CLI. Odds are you'll use Grunt in multiple projects, so you'll want to install that globally using -g.
Now lets say you're working on a project and your project is going to require a module such as Express. You would cd to your projects root directory and install the module without -g.
Here is a more in depth explanation.
install means the module will be created in your local node_modules folder which is highly recommended for anything your application relies on (for versioning, amongst other reasons).
install -g means install the module globally on your machine. This is generally only recommended to use with modules that perform some task unrelated to the execution of your application.
Simple examples of this are Yeoman generators, the Express generator, PhantomJS, etc.
There is an official blog post about it here
The only difference is npm install mod will install it in your local directory. Let's say you are working in 'projectA' directory. So
> npm install mod
will install "mod" in
> projectA/node_modules/mod/
so any .js file inside projectA can use this module by just saying require('mod')
whereas 'npm install mod -g` will install it globally in the user's node module directory. It will be somewhere in
> /usr/bin/npm/node_modules/modA
you can use this module anywhere in any of your project, in addition to that if there is any terminal command is there in 'modA'. It will be accessible from you terminal directory.
> modA --version
> version 1.1

Categories

Resources