Skip to content

Commit 72c3a55

Browse files
committed
README: explain how to use with library code
1 parent 7aada2c commit 72c3a55

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ require('my_private_module');
2727
import module from 'my_private_module'
2828
```
2929

30-
**WARNING:** This module should not be used in other npm modules since it modifies the default `require` behavior! It is designed to be used for development of final projects i.e. web-sites, applications etc.
30+
**WARNING:** Although this module was designed for end-user projects (websites, etc.), it _can_ be used with library code, but this requires some care and a bit of work. Please see [Using in a library](#using-in-a-library).
3131

3232
## Install
3333

@@ -160,6 +160,16 @@ Unfortunately, `module-alias` itself would not work from Jest due to a custom be
160160

161161
More details on the [official documentation](https://jestjs.io/docs/en/configuration#modulenamemapper-objectstring-string--arraystring).
162162

163+
## Using in a library
164+
165+
You can use `module-alias` within a library, however there are a few things to take into consideration.
166+
167+
1. As the aliases are global, you should make sure your aliases are unique, to avoid conflicts with end-user code, or with other libraries using module-alias. For example, you could prefix your aliases with '@my-lib/', and then use require('@my-lib/deep').
168+
2. The vanilla "register" mechanism does not work, you cannot simply use `require('module-alias/register')`, as it tries to find package.json in either the current working directory of your node process, or two levels down from node_modules/module-alias. It is extremely likely that this is end-user code. So, instead, your should either register aliases manually with moduleAlias.addAlias, or using something like `require('module-alias')(__dirname)`.
169+
170+
Here is an [example project](https://github.com/Kehrlann/module-alias-library).
171+
172+
163173
## Known incompatibilities
164174

165175
This module does not play well with:

0 commit comments

Comments
 (0)