Comments (8)
I figure out another workaround. You can use yarn's nohoist
option to make sure that app-root-path doesn't get hosted to the top "project root" directory.
If you put this in your subproject/package.json and re-install packages (I usually do yarn install --force
) you should end up with app-root-path in subproject/node_modules which then should cause a correct lookup and find your ormconfig.js:
{
"workspaces": {
"nohoist": [
"typeorm/**",
"typeorm"
]
}
}
from node-app-root-path.
I just bumped into this as well. As a workaround, you can use the ConnectionOptionsReader directly to get options from any folder/file you like.
My connection stuff is still a bit messy, but I'll post it here as an example. Note that I apply the name "default" to my config so the connectionOptionsReader.get
call will work.
import * as fs from 'fs';
import * as path from 'path';
import { createConnection, Connection, ConnectionOptionsReader } from "typeorm";
const connectionOptionsReader = new ConnectionOptionsReader({
// TODO: Maybe look in every folder up from CWD
root: process.cwd()
});
let connectionOptions;
let connection;
export default async function connect (): Promise<Connection> {
if (!connection) {
connectionOptions = {
...(await connectionOptionsReader.get(process.env.TYPEORM_CONNECTION || 'default')),
entities: [__dirname + "/entity/**/*.js"],
migrations: [__dirname + "/migration/**/*.js"],
subscribers: [__dirname + "/subscriber/**/*.js"]
};
connection = createConnection(connectionOptions);
}
return connection;
}
Checking in process.cwd()
seemed to mimic the behavior of the typeorm cli commands (like typeorm migration:generate
), so it was convenient to have this behavior be consistent.
I also found it convenient to set the entities
and other paths here so that I could consume this package from other repos, with their own ormconfig.js files, and not have to keep setting up these paths. Not sure if I'm doing all of this right, but at least I can load my configs in my monorepo now!
Hope that's helpful
from node-app-root-path.
Oof, that seems like a nightmare to resolve. I think the first step is to try to enumerate all the different cases that we need to account for and the expected behavior. Maybe you could start there and see if anyone has comments?
from node-app-root-path.
I recently ran into this issue with Typeorm specifically as well.
@sgronblo's solution was great for Yarn v1, but if you're using Yarn v2–v3, you will likely need a .yarnrc.yml
(I added it to my workspace directory) with the following:
nodeLinker: node-modules
nmHoistingLimits: workspaces
Now run yarn
and you should see the node_modules
folder inside your workspace directory, with Typeorm in node_modules
as well.
(You can now safely remove the deprecated nohoist
code from your root package.json
)
nohoist
is deprecated in v2+ and replaced by nmHoistingLimits
(Docs).
This article also explains more about Yarn's decision to change away from nohoist
.
P.S. if you're switching from Yarn v1 to v2+, I recommend reading their step-by-step upgrade guide.
from node-app-root-path.
Does anyone have any recommendations on how to handle workspaces?
from node-app-root-path.
I suppose we could read the package.json
file in the discovered root path, and look for a "workspaces"
key, and adjust the behavior if that exists…
from node-app-root-path.
@inxilpro This is my goto package and just realised it's not bulletproof under yarn workspaces! Would be keen to help with this.
regarding looking for workspaces
. Child projects can also have workspaces
such as for nohoist
"workspaces": {
"nohoist": ["react-native", "react-native/**"]
}
Do you have any other ideas of how we could solve this?
from node-app-root-path.
It should be safe to just check the package.json
if there's workspaces
that's Array.isArray()
or workspaces.packages
that's Array.isArray()
we can safely assume this is the root package of the mono repo. Should work well for yarn which is what I'm familiar with.
And if not, we keep traversing up the tree until we find a package.json
that fits the criteria
from node-app-root-path.
Related Issues (20)
- node_modules isn't cool anymore, what to do HOT 12
- Webpack warning: Critical dependency: the request of a dependency is an expression HOT 4
- npm package.json version != github repo version HOT 1
- Path resolution in npm linked modules report module and not root app path. HOT 6
- New version broke webpack build with TypeORM HOT 3
- Auto including index.js HOT 1
- PNPM support?
- Latest NPM release failed (3.0.0) HOT 1
- What constituted the bump in major version? HOT 1
- Incompatible with Browserify HOT 1
- Mistenken added issue HOT 1
- Issues
- Issues
- Androidapk merge HOT 1
- wrongful access
- Google Cloud Functions reports incorrect path HOT 3
- Need Yarn 2 monorepo support
- Package throws an error when imported in an ES module installed globally on Ubuntu HOT 1
- ReferenceError: __dirname is not defined in ES module scope HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-app-root-path.