Ubuntu 17环境下创建文件夹中yarn/npm包安装后消失问题
Hey there, I've run into this exact issue a few times when working with monorepos or fresh project directories—let's break down what's probably happening and how to track down your missing react and react-dom packages:
1. You're likely in a Yarn Workspace (Monorepo Setup)
Since you're working inside bundler-app/packages/myfolder, it's common for the root bundler-app directory to have a Yarn Workspaces configuration. This setup tells Yarn to install all dependencies from sub-packages into a single node_modules folder at the root of your project (to save space and share dependencies across packages).
- Head up to the
bundler-appdirectory and check if there's anode_modulesfolder there—yourreactandreact-dompackages are almost certainly inside it. - Verify this by checking the root
package.jsonfor aworkspacesfield, which will look something like this:{ "workspaces": [ "packages/*" ] }
This is intentional behavior for monorepos, so don't worry—your packages are safe, just centralized!
2. Your myfolder Directory Doesn't Have a package.json
Yarn needs a package.json file in the directory where you run yarn add to know where to install local dependencies. If you didn't initialize a package first, Yarn will walk up the directory tree and install the packages in the nearest parent directory that has (or can create) a package.json.
- Check if
packages/myfolderhas apackage.jsonfile. If not:- Run
yarn init -yinsidemyfolderto generate a basicpackage.json - Re-run
yarn add react react-dom—this time, Yarn will create anode_modulesfolder directly inmyfolderand install the packages there.
- Run
- If you already checked the root
bundler-appdirectory, you'll probably find apackage.jsonthere withreactandreact-domlisted in its dependencies (Yarn created it automatically when you ran the first install).
3. (Unlikely) Accidental Global Install
While yarn add defaults to local installation, it's worth ruling out a global install just in case. Run yarn global dir to see where Yarn stores global packages—if you don't see react there, you can cross this off your list.
Quick Recap
9 times out of 10, your packages are either in the root bundler-app/node_modules (thanks to workspaces) or in a parent directory's node_modules because you didn't initialize a package.json in myfolder. Follow the steps above, and you'll find them in no time!
内容的提问来源于stack exchange,提问作者faraz

