How to install/symlink packages in monorepos from root node_modules in yarn workspace - npm

I have to use the cached node_modules at root level and use it to add symlinks to all the mono-repo packages as the current command, yarn install ignores the available root level node_modules and re-install them from server instead. Any way I can save time here?
Repo Structure
- node_modules # available already in repo
- packages/
- package1
- node_modules # this to installed/symlink from root node_modules
- package.json
- package2
- node_modules # this to installed/symlink from root node_modules
- package.json

Related

Do packages hoisted still install?

In regard to hoisting, I have this workspace structure:
root
-apps
-myApp
package.json
-packages
-myPackage
package.json
I have "react-native-reanimated": "~2.12.0" inside dependencies in both package.json of myApp and myPackage.
When hoisting, I expect to only see one react-native-reanimated folder in the root node_modules, but I see one in myApp and in one the root (by not in myPackage).
Isn't hoisting supposed to install a package (with the same version) in node_modules in the root node_modules and nowhere else?

npm init doesn't get all packages in node_modules folder

I have a project with a node_modules folder. Something unexpected happened and i had to delete my package.json and package-lock.json files.
Now i need to get my project's packages and dependencies etc.. into a new package.json file.
Unfortunately, running npm init doesn't get all packages that are in the node_modules directory.
For example, react and react-dom are installed and present in node_modules but don't get included in the generated package.json file by npm init; alongside other packages.
What can i do to make all my packages appear in package.json and also if possible in package-lock.json ?

how to convert package-lock.json to yarn.lock?

I have an project with package-lock.json file.
Now, I want to generate yarn.lock file based on package-lock.json file or existed node_modules of project.
How can I do this? thanks.
You can use yarn import to generate yarn.lock file from an existing npm-installed node_modules folder.
yarn import aims to alleviate this challenge by generating a yarn.lock file in one of two ways:
Using the dependency tree in an existing package-lock.json file created by npm install
If no such file exists, using the versions found inside node_modules according to normal require.resolve() resolution rules.
For example:
☁ api [develop] ⚡ yarn import
yarn import v1.17.3
info found npm package-lock.json, converting to yarn.lock
warning jest > jest-cli > jest-config > jest-environment-jsdom > jsdom > left-pad#1.3.0: use String.prototype.padStart()
success Saved lockfile.
✨ Done in 21.56s.
You can use the tool synp to convert back and forth:
synp --source-file yarn.lock # will create package-lock.json
synp --source-file package-lock.json # will create yarn.lock
as of yarn 1.7.0 (2018-06-04) you can use yarn import to convert npm package-lock.json to yarn.lock instead of having to npm install first for node_modules and tell yarn to take a looks at that
https://yarnpkg.com/lang/en/docs/migrating-from-npm/
https://yarnpkg.com/blog/2018/06/04/yarn-import-package-lock/

Load an npm package from local directory without copying unnecessary files/folders such as node_modules

Let's imagine I have to develop an npm package, my-package, which needs to be imported and used by my-project.
While developing my-package I need to install and use other packages from npm. At the end of the development of my-package, I bundle all the relevant files in the dist directory. Since I do not want to have all the files in the node_modules folder as part of the package published, I specify in .gitignore (or .npmignore) to exclude node_modules folder. So the final structure of the folder of my-package looks something like
my-package
dist
... // all the code of the package to be distributed
node_modules
... // all the node modules
src
... // all source files
package.json
.gitignore
Now I publish my-package on npm repository and then import it in my-project, with the command npm i my-package.
As a result of such import, the structure of the directory hosting my-project is something like
my-project
... // stuff
node_modules
my-package
dist
... // all the code of the package to be distributed
src
... // all source files
package.json
As expected, no node_modules folder is imported under my-package folder.
Since I am the developer of both my-package and my-project, during development I would like to import my-package from the local path with a command which could look like
npm -i ../my-package
If I proceed like this, the result I see is that all the files and folders stored under my-package directory are copied under the node_modules folder of my-project, i.e. .gitignore (or .npmignore) exclusions are not considered. The net result is a structure such as
my-project
... // stuff
node_modules
my-package
dist
... // all the code of the package to be distributed
node_modules
... // all stuff imported by my-package
src
... // all source files
package.json
... // any other file under my-package
Is there a way to install from local path only the relevant files of my-package as it happens if the package is installed from the npm repository?
We had the same problem today. We figure it out by adding to the package.json of the local package
“build”: “yarn run clean && yarn build-components && yarn pack”
And then to the project that is using the local package we added to the dependencies of the package.json
“yourpackage”: “file:../your-tgz.tgz”,
hope it helps
Note: if you are using yarn you might have problems with the cache.

How to install dependency into package folder?

I have a following folder structure:
- root/
- app/
- src/
- node_modules/
I published some package on npmjs and in the moment when i type in /root folder npm install my_package -S dependency my_package installs in root folder node_modules and i would like to install them in the package folder ie node_modules/my_package /node_modules
How to achieve it?
Cycles are handled using the property of node's module system that it walks up the directories looking for node_modules folders. So, at every stage, if a package is already installed in an ancestor node_modules folder, then it is not installed at the current location.
-npm docs