Comments (14)
Yes, The default is the current working dir (usually where your package.json is), because Node wont tell us the dir of the file unless you explicitly do so with '__dirname' which you may pass to opts.cwd 😀
This should do the trick:
container.loadModules([
'../src/lib/*.js',
'../src/services/*.js'
], { cwd: __dirname })
Also, if you haven't already, I suggest you read up on lifetime management - one of the (IMO) best features of Awilix when writing API's/web apps - as well as the special overload for loadModules
:
container.loadModules([
['../src/lib/*.js', Lifetime.SINGLETON],
['../src/services/*.js', Lifetime.SCOPED]
], { cwd: __dirname })
from awilix.
Yeah thats what I was thinking - I've personally noticed more people naming their files after their default export, which is why that is the default convention in Awilix.
If you want to continue using filenames with dashes, you can specify opts.formatName: 'camelCase'
for loadModules
- it will use the camel-case
module to convert your dashed version to camel case. 😄
from awilix.
That one was the earliest pattern and was more or less converted from awilix v1 to v2, but I recommend using this instead (it's less confusing IMO)
export default function makeUserService ({ someService }) {
return {
getUser: () => someService.doStuff()
}
}
from awilix.
Yes, the last one registered wins.
from awilix.
does opts.cwd
have something to do with this?
from awilix.
I just changed cwd to __dirname, but didn't help, I still get the same error.
from awilix.
Could I see the contents of the file that contains the function you want to register?
Please keep in mind only the default export is registered when using loadModules
.
from awilix.
Here's a working project boilerplate using loadModules
: https://github.com/jeffijoe/koa-es7-boilerplate/blob/master/src/lib/configureContainer.js
from awilix.
Thank you so much. So it was a combination of export default
and file name with dashes!
from awilix.
the file name is not an issue, this is what I am doing:
const { sendValidResponse, sendErrorResponse } = container.resolve('response-handlers')
from awilix.
Right - I'm guessing you just found out that only the default export is registered. 😄
The reason for this is that I don't want to register any other exported members since they are likely just going to pollute the container.
Also, please keep in mind you don't have to use loadModules
- the following is perfectly acceptable as well:
// response-handlers
export function sendValidResponse ({ db }) {
return async (req, res) => {
res.status(200).send(await db.get('something'))
}
}
// dependencies.js
import * as handlers from '../src/response-handlers'
import mapValues from 'lodash/mapValues'
const container = createContainer()
.registerFunction(
// Map to scoped registration
mapValues(handlers, (handler) => [handler, { lifetime: Lifetime.SCOPED }])
)
Basically using lodash/mapValues
to register all handlers with scoped lifetime.
Hope this helps.
from awilix.
yes, I was using that version of the code before, but wanted to take advantage of passing the modules as arguments from this example:
export default function (opts) {
return {
getUser: getUser.bind(null, opts),
}
}
from awilix.
what would happen if I do something like this:
container.loadModules([
'../src/services/user.js',
'../src/test/mocks/user.js'
])
will the second user.js overwrite the first?
from awilix.
You can use this to your advantage with scoping! E.g. register a default logger
, but overwrite it with a scoped version that also collects request and user info. 😎
Welcome to the world of DI in Node with Awilix. 😄
from awilix.
Related Issues (20)
- awilix object is undefined when using default import, but named import of functions work HOT 1
- Support for ESM modules HOT 3
- Is it possible to reinforce constructor param name/types? HOT 1
- Can't resolve HOT 4
- How to unregister a dependency from the container? HOT 6
- Default value not working in class constructor in Proxy mode HOT 2
- asFunction -> asClass Cyclic dependency HOT 2
- Awilix TypeError: Cannot read properties of undefined (reading 'cache') HOT 2
- Calling `loadModules` does not register all the file paths HOT 2
- Is it possible to detect / error on singletons dependent on transient or scoped registrations? HOT 1
- Awilix 10 type exposure HOT 1
- [QUESTION] Userland async initializers HOT 7
- replace camel-case dependency HOT 3
- Possible valid use-case for disabling strict mode for specific resolver HOT 2
- Critical dependency: the request of a dependency is an expression HOT 3
- Add TypeScript support HOT 1
- [QUESTION] - Awilix Fails to Autoload Modules in Vitest Tests with TypeScript Setup HOT 1
- How can I get scope inside instance? HOT 14
- [QUESTION] - How to find registered but not used containers? HOT 3
- Support `loadModules` for apps running in the Browser (with Vite) HOT 14
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 awilix.