Share node_modules installation between sub-projects, while maintaining separate package.json files - npm

I have the following folder structure:
project
├───components
│ ├───component-one
│ │ package.json
│ │
│ └───component-two
│ │ package.json
│ │
│ └───node_modules
├───node_modules
└───package.json
Project root project folder contains package.json and is intended to have various infrastructural modules installed (Gulp, for example, as the build is centralized).
Each component under components folder is eventually, after build and whatnot, is deployed somewhere to be consumed by an application - using the usual npm install from folder or tarball. For that reason, each component must maintain its own dependencies in its own package.json.
Going the trivial route, installing node_modules into each of the component folders would lead to a crazy amount of duplication, as there may be 100s of components, each installing mostly the same dependencies.
Ideally I would like to:
run, for example, npm install -D <module> in component-one folder
have package.json in that folder updated with the <module>
have <module> installed in the project folder
This can be achieved, to some extent, running (on Windows, in this case) mklink /D node_modules ..\..\node_modules from component-one to create a symlink.
However, symlinks are fragile and finicky, so I'd like to avoid that solution.
Is there an npm solution, via npm link or something that I am missing?

Related

How to use npm workspace

I develop my app by using npm workspace.but I am totally novice to this concept so that I would like to understand npm workspace behavior
.
├── package.json
└── packages
├── a
│ └── package.json
├── b
│ └── package.json
└── c
└── package.json
package.json
{
"workspaces": [
"packages/*"
]
}
My question is
①what happens when I run npm install in root directory ?
All the packages are installed in each repository ? or root directory ?
②npm install in each project is not recommended ?
if someone has opinion, will you please let me know.
Thanks
So long as all packages have the same version of dependencies listed (e.g. thing#1.0.0), then npm install run from the root of your repository will add the dependency into the root node_modules directory.
If one of your packages (e.g. packages/c) has a different version of a dependency (e.g. thing#1.0.1) then that would appear in packages/2/node_modules)
In theory you are using npm workspaces to ensure some consistency across packages, therefore ensuring your versions are consistent will mean node_modules is only ever created in the root of your repository

Npm script to run both child directory folders?

How do I write a script in my Parent Folder's package.json file so that when I run npm install it installs the node modules in each folder and npm start will go to each folder and run npm start
The Frontend and Backend folder both use npm start to start up and I want to somehow do the same in the parent folder to simultaneously start both
This is the file structure:
ParentFolder
├── package.json . <--- npm install && npm start scripts
├── FrontEnd
│ ├── /node_modules
│ ├── package.json
│ └── index.js
├── Backend
│ ├── /node_modules
│ ├── package.json
│ ├── routes.js
│ └── server.js.js
Installing in two directories is easy with find
find ./*/* -maxdepth 1 -name package.json -execdir npm install \;
This looks in each directory for a package.json and executes npm install;
npm start becomes a bit harder. At least on Windows using Cygwin, I wanted to do:
npm --prefix ./FrontEnd start ./FrontEnd & npm --prefix ./Backend start ./Backend
But it wasn't actually running in the background like I expected and FrontEnd was the only one that actually started. Depending on your start script this could work for you.
Possible solutions for this could be concurrently or npm-run-all -p.

webpack a module that contains node_modules

Question
I want to make changes to an open source JavaScript library. Using webpack and npm and keeping everything local, what options do I have to make changes to a local module and import it into project in place of an public npm module downloaded from the registry? The local module and consuming app will also be source controlled under two separate git repositories.
Problem
I am testing this within an Aurelia app, but I think it is a webpack and npm problem. I have an app called my-app which has a dependency on aurelia-binding. I wish to make local changes to aurelia-binding and push them to a fork in GitHub. My project structure looks like this:
└───my-app
├───.git
├───dist
├───node_modules
│ └───aurelia-binding
| ├───.git
│ ├───dist
│ └───src
└───src
When built and run, everything works as expected. To make a change to aurelia-binding and test them in my-app, I need to install its dependencies and build it. This results in a structure like this:
└───my-app
├───dist
├───node_modules
│ └───aurelia-binding
| ├───.git
│ ├───dist
| ├───node_modules
| | └───dependencies...
│ └───src
└───src
When the node_modules are installed on the dependency, webpack throws an error at runtime. The error may look like an issue with the aurlia module, but I do not believe this is the case.
Unhandled rejection Error: Error invoking SVGAnalyzer. Check the inner error for details.
------------------------------------------------
Inner Error:
Message: __WEBPACK_IMPORTED_MODULE_1_aurelia_pal__.a.createElement is not a function
I have also tried this using npm link with the library cloned next to my-app instead of within in but got the same result.
I used the following addition to the webpack.config.js to be able to use npm/yarn linked packages. It forces webpack to resolve the modules only using the main node_modules and not the "nearest" ones.
resolve: {
extensions: [".ts", ".js"],
symlinks: false,
modules: ["ClientApp", path.resolve('node_modules')],
}
Where "ClientApp" is the folder where my aurelia app is located. The symlinks parameter is used to prevent webpack from resolving the symlinked path to an absolute path.
A word of warning, with this setup you circumvent version checking by npm so you are responsible for linking a compatible version.

what is npm doing differently when loading a project with a github url vs the usual npm install?

There's an npm module I'm using, that works fine in my project when installed with npm and its semver, but that fails with the below error when I try to install it with its github url.
The error: Uncaught Error: removeComponentAsRefFrom(...): Only a ReactOwner can have refs. You might be removing a ref to a component that was not created inside a component'srendermethod, or you have multiple copies of React loaded
I'm loading the same version of the module, not changing any build configs, the only difference is loading with semver vs github url.
I should say, the module's lib folder is not checked into github. So I do run a build of the module manually.
I'm using webpack for the build. I suspect its a problem with the copies of react being loaded, but that doesn't seem to have changed, and webpack seems to be handling duplicates.
npm ls react:
├── react#15.6.1
├─┬ react-color#2.2.0
│ └─┬ reactcss#0.4.6
│ └── react#0.14.9
└─┬ react-with-context#1.2.1
└── react#15.6.1 deduped
A little confused, any tips appreciated
(If that makes any difference, the error seems to be thrown from a dependency of the module that I'm testing. And in both modules (the one I'm testing + its dependency), react is listed as a peerDependency package.json)

React Native cannot find entry file in any of the roots

Description
Cloned a repo to a new computer and getting the following error:
swipes#0.0.1 start C:\a\swipes-api\mobile
node node_modules/react-native/local-cli/cli.js start
Scanning 722 folders for symlinks in C:\a\swipes-api\mobile\node_modules (30ms)
┌────────────────────────────────────────────────────────────────────────────┐
│ Running packager on port 8081. │
│ │
│ Keep this packager running while developing on any JS projects. Feel │
│ free to close this tab and run your own packager instance if you │
│ prefer. │
│ │
│ https://github.com/facebook/react-native │
│ │
└────────────────────────────────────────────────────────────────────────────┘
Looking for JS files in
C:\a\swipes-api\mobile
error: bundling: NotFoundError: Cannot find entry file index.android.js in any of the roots: ["C:\\a\\swipes-api\\mobile"]
at DependencyGraph._getAbsolutePath (C:/a/swipes-api/mobile/node_modules/react-native/packager/src/node-haste/DependencyGraph.js:280:11)
at DependencyGraph.getDependencies (C:/a/swipes-api/mobile/node_modules/react-native/packager/src/node-haste/DependencyGraph.js:218:26)
at Resolver.getDependencies (C:/a/swipes-api/mobile/node_modules/react-native/packager/src/Resolver/index.js:107:27)
at C:/a/swipes-api/mobile/node_modules/react-native/packager/src/Bundler/index.js:591:37
at next (native)
at step (C:\a\swipes-api\mobile\node_modules\react-native\packager\src\Bundler\index.js:12:445)
at C:\a\swipes-api\mobile\node_modules\react-native\packager\src\Bundler\index.js:12:605
at process._tickCallback (internal/process/next_tick.js:103:7)
Bundling `index.android.js` 0.0% (0/1), failed.
The path is correct where it's searching from. I've been stuck with this for the whole day now.
Is this somehow because there are double backslashes in the path to the index file?
npm start -- --reset-cache results in the same error.
Running yarn start after react-native run-android results in the same issue as well.
npm run start -- --root C:\a\swipes-api\mobile - No result.
This does not seem to be connected to the previous issue in the RN 0.45.0.
Additional Information
React Native version: 0.45.1
Platform: Android
Development Operating System: Windows
Edit:
All new projects with react-native init result in the same issue. Seems to be an issue with the config of my computer or npm, but I can't seem to track down why this happens.
Any ideas?
I have this and some like this error on windows.
Updating the npm and the node helped me on this issue.
currently i have:
C:\usr\Far>node -v && npm -v
v6.11.1
3.10.10
Many trials to run was not successful at all until I do not update. Anyway some errors like this appear time to time. Just restart. Also helpful to do before restart (but may be not need, just restart):
sh -c 'rm -rf $TMP/react*'
npm cache clean
Where $TMP is your system tmp directory, (check for existence react temp files there)
If you add or update some packages or some like that modifications concerning node_modules directory need to do also:
rm -rf node_modules && npm install