You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -140,59 +140,114 @@ and variables which are outside all modules.
140
140
141
141
.. data:: package.path
142
142
143
-
This is a string that Tarantool uses to search for Lua modules,
144
-
especially important for ``require()``.
145
-
See :ref:`Modules, rocks and applications <app_server-modules>`.
143
+
Get file paths used to search for Lua :ref:`modules <app_server-modules>`.
144
+
For example, these paths are used to find modules loaded using the ``require()`` directive.
145
+
146
+
See also: :ref:`package.searchroot() <other-package_searchroot>`
146
147
147
148
.. _other-package_cpath:
148
149
149
150
.. data:: package.cpath
150
151
151
-
This is a string that Tarantool uses to search for C modules,
152
-
especially important for ``require()``.
153
-
See :ref:`Modules, rocks and applications <app_server-modules>`.
152
+
Get file paths used to search for C :ref:`modules <app_server-modules>`.
153
+
For example, these paths are used to find modules loaded using the ``require()`` directive.
154
+
155
+
See also: :ref:`package.searchroot() <other-package_searchroot>`
154
156
155
157
.. _other-package_loaded:
156
158
157
159
.. data:: package.loaded
158
160
159
-
This is a string that shows what Lua or C modules Tarantool
160
-
has loaded, so that their functions and members are available.
161
-
Initially it has all the pre-loaded modules, which don't need
162
-
``require()``.
161
+
Show Lua or C modules loaded by Tarantool, so that their functions and members are available.
162
+
``loaded`` shows both pre-loaded modules and modules added using the ``require()`` directive.
163
+
164
+
See also: :ref:`package.searchroot() <other-package_searchroot>`
165
+
166
+
.. _other-package_searchroot:
167
+
168
+
.. function:: package.searchroot()
169
+
170
+
Return the current search root, which defines the path to the root directory from which dependencies are loaded.
171
+
By default, the search root is the current directory.
172
+
173
+
.. NOTE::
174
+
175
+
The current directory is obtained using :ref:`debug.sourcedir() <debug-sourcedir>`.
176
+
177
+
**Example**
178
+
179
+
Suppose the application has the following structure:
180
+
181
+
.. code-block:: none
182
+
183
+
/home/testuser/myapp
184
+
├── .rocks/share/tarantool/
185
+
│ └── foo.lua
186
+
├── init.lua
187
+
└── modules
188
+
└── bar.lua
189
+
190
+
In this case, modules are placed in the same directory as the application initialization file.
191
+
If you :ref:`run the application <app_server-launching_app_binary>` using the ``tarantool`` command from the ``myapp`` directory, ...
192
+
193
+
.. code-block:: console
194
+
195
+
/home/testuser/myapp$ tarantool init.lua
196
+
197
+
... the search root is ``/home/testuser/myapp`` and Tarantool finds all modules in this directory automatically.
198
+
This means that to load the ``foo`` and ``modules.bar`` modules in ``init.lua``, you only need to add the corresponding ``require`` directives:
199
+
200
+
.. code-block:: lua
201
+
202
+
-- init.lua --
203
+
require('foo')
204
+
require('modules.bar')
205
+
206
+
Starting with :doc:`2.11.0 </release/2.11.0>`, you can also run the application using the ``tarantool`` command from the directory other than ``myapp``:
207
+
208
+
.. code-block:: console
209
+
210
+
/home/testuser$ tarantool myapp/init.lua
211
+
212
+
In this case, the path to the initialization file (``/home/testuser/myapp``) is added to search paths for modules.
213
+
214
+
To load modules placed outside of the path to the application directory, use :ref:`package.setsearchroot() <other-package_setsearchroot>`.
Set the search root. The search root is the root directory from
169
-
which dependencies are loaded.
220
+
Set the search root, which defines the path to the root directory from which dependencies are loaded.
221
+
By default, the search root is the current directory (see :ref:`package.searchroot() <other-package_searchroot>`).
170
222
171
-
:param string search-root: the path. Default = current directory.
223
+
:param string search-root:a relative or absolute path to the search root. If ``search-root`` is a relative path, it is expanded to an absolute path. You can omit this argument or set it to :ref:`box.NULL <box-null>` to reset the search root to the current directory.
172
224
173
-
The search-root string must contain a relative or absolute path.
174
-
If it is a relative path, then it will be expanded to an
175
-
absolute path.
176
-
If search-root is omitted, or is box.NULL, then the search root
177
-
is reset to the current directory, which is found with debug.sourcedir().
225
+
**Example**
178
226
179
-
Example:
227
+
Suppose external modules are stored outside the application directory, for example:
180
228
181
-
Suppose that a Lua file ``myapp/init.lua`` is the project root. |br|
182
-
Suppose the current path is ``/home/tara``. |br|
183
-
Add this as the first line of ``myapp/init.lua``: |br|
184
-
:code:`package.setsearchroot()` |br|
185
-
Start the project with |br|
186
-
:code:`$ tarantool myapp/init.lua` |br|
187
-
The search root will be the default, made absolute: ``/home/tara/myapp``.
188
-
Within the Lua application all dependencies will be searched relative
189
-
to ``/home/tara/myapp``.
229
+
.. code-block:: none
190
230
191
-
.. _other-package_searchroot:
231
+
/home/testuser/
232
+
├── myapp
233
+
│ └── init.lua
234
+
└── mymodules
235
+
├── .rocks/share/tarantool/
236
+
│ └── foo.lua
237
+
└── modules
238
+
└── bar.lua
192
239
193
-
.. function:: package.searchroot()
240
+
In this case, you can specify the ``/home/testuser/mymodules`` path as the search root for modules in the following way:
241
+
242
+
.. code-block:: lua
243
+
244
+
-- init.lua --
245
+
package.setsearchroot('/home/testuser/mymodules')
246
+
247
+
Then, you can load the ``foo`` and ``bar`` modules using the ``require()`` directive:
194
248
195
-
Return a string with the current search root.
196
-
After ``package.setsearchroot('/home')`` the returned
0 commit comments