NaviServer Built-in Commands – 5.2.0a
ns_module - Module context, metadata, Tcl dependencies, and module reload
The ns_module command describes the module whose Tcl library is currently being initialized. NaviServer sources "init.tcl" before the other Tcl files in that module's library.
Configuring a Tcl module does not by itself establish that it is loaded. NaviServer warns when its shared and private library directories contain no Tcl files. ns_server modules lists a Tcl module only after its startup files have been sourced successfully; metadata declarations during startup remain available to the initializing module.
Called at the top level of a module definition file, ensures that the named prerequisite files have been sourced before execution continues. Files are relative to the specified module, or the current module when -module is omitted. With -module and no filenames, requires all top-level *.tcl definition files of that module. The module must be configured for the current virtual server. Circular dependencies raise an error. Private files override shared files with the same relative name. Each file is loaded once during startup, including files loaded early through dependencies. Equivalent relative names, such as base.tcl and ./base.tcl, share one dependency identity. Redundant separators and current-directory components are normalized before dependency registration and loading. Absolute paths and parent-directory components are rejected. init.tcl cannot be required.
Declare prerequisites from the base Tcl library. Core files can also use require without -module to name other core files. Core and module dependencies share one graph, so reloading a core file restores affected module definitions. An empty module name identifies core files internally; no fictitious module is registered.
Return a dictionary of successfully sourced files for the specified module or the current server's base Tcl library. Each filename maps to a dictionary with path (the effective source path) and reloadable (a boolean). Startup-only "init.tcl" files and core bootstrap files appear in this inventory with reloadable false. Filenames retain logical identity when a private file overrides a shared file.
Without -core, exactly one module name is required. With -core, one or more core filenames are required; additional filenames are accepted only in this form.
With -core, reload explicitly selected, recorded core files and their transitive dependents through ns_eval -sync. There is no implicit reload-all operation. Core reload is enabled for "charsets.tcl", "compat.tcl", "form.tcl", "http.tcl", "ns_crypto.tcl", "ns_stats.tcl", "privacy.tcl", "sendmail.tcl", "tclpro-debug.tcl", "util.tcl", and "win32.tcl". The debugger and Windows helper files only redefine procedures; reloading them does not connect to a debugger or create directories. Reloading "ns_stats.tcl" uses NX soft recreation, which must be enabled by the NX setup, to preserve live instances, the active sampler, its baseline, history configuration, and scheduled callbacks. Startup configuration is not reapplied. Private overrides of these files must also support repeated evaluation. Bootstrap files, including "init.tcl", "nstrace.tcl" and "nsd/init.tcl", are excluded. If an affected core dependent is startup-only, the operation fails before sourcing any files. Returned affected-file keys use an empty module name for core files.
Without -core, re-sources the module's top-level Tcl definition files, plus previously loaded files in that module and all files transitively dependent on them, including files in other modules. Prerequisites are sourced before their consumers and each affected file is sourced once per execution of the loader. Dependencies are recorded by require and refreshed when files are sourced again. Whole-module requirements record the definition files present when evaluated. The operation uses ns_eval -sync for blueprint publication and normal interpreter propagation. It waits for publication, not for all active interpreters to adopt the new blueprint. The result is a list of affected module file pairs; recursive prerequisite loading can change the actual source order. Source errors stop the operation and propagate to the caller; the failed operation does not publish a new blueprint. Module context and dependency bookkeeping are restored on errors. Arbitrary effects of executed Tcl code, such as shared-variable changes, cannot be rolled back. Files removed since loading cause an error rather than silently leaving stale definitions. init.tcl retains its existing startup behavior and is excluded from reload. Other Tcl files must be suitable for repeated evaluation. Procedures removed from a source file are not automatically deleted from interpreters. Do not register schedules or create process resources in reloadable files unless the initialization is explicitly safe to repeat. The existing ns_eval implementation can evaluate the loader both in the caller and in a worker interpreter.
Return or set the current module name. NaviServer establishes this context before sourcing its Tcl library.
Return or set the shared module library directory.
Return or set the private, server-specific module library directory. The library subcommand is an alias for private.
Return the current module's version, or declare it when value is supplied.
Return the current module's source revision/build tag, or declare it when value is supplied.
Clear the initialization context. This does not remove registered metadata.
Version and tag declarations require nonempty values and are accepted only during server startup. Each field can be declared once; repeating a declaration raises an error, even when the value is identical. Fields already supplied by a binary module's Ns_ModuleGetInfo callback cannot be replaced. Mixed C/Tcl modules should use their existing callback metadata.
Declared fields are retained in the loaded-module registry and returned by ns_server modules. Tcl-module metadata is scoped to the virtual server being initialized. Clearing the context or creating a new interpreter does not clear these fields. Reading a field that has not been declared returns an empty string; accessing version or tag without a current module raises an error.
# In a Tcl module's init.tcl: ns_module version 0.1 ns_module tag nsmcp-0.1-12-g0123456789ab # After startup: dict get [ns_server modules] nsmcp
The module build include "Makefile.module" can generate an entry point from TCL_INIT (a template file), writing TCL_INIT_FILE (default "init.tcl"). Set NS_MODULE_VERSION to the release version and use @NS_MODULE_VERSION@ and @NS_MODULE_TAG@ in the template. The build tag defaults to git describe --abbrev=12 --always --dirty=+. An explicit NS_MODULE_TAG overrides it. Without Git, the generated ns_module tag declaration is retained, or unknown is used when no recorded tag exists. Include the generated entry point in distribution archives so installation preserves the source identity. The generated file is added to the shared Tcl files installed by make install.
# In a module definition file: ns_module require -module mod1 file1.tcl file2.tcl ns_module require -module mod2 ns_module require foo1.tcl foo2.tcl # From an administrative interpreter: ns_module reload mod1 ns_module reload -core http.tcl ns_module files -core