diff --git a/postModuleLoad.cmd b/postModuleLoad.cmd
index 8cc206ab612c3a5648e3fe555d9220453e68cc5e..276e78289d0599be86756b39186a7bf36a676d20 100644
--- a/postModuleLoad.cmd
+++ b/postModuleLoad.cmd
@@ -1 +1 @@
-dbLoadRecords("$(require_DIR)/db/moduleversion.template","IOC=$(IOC),MODULE=$(MODULE),VERSION=$($(MODULE)_VERSION=)"
+dbLoadRecords("$(require_DIR)db/moduleversion.template","IOC=$(IOC),MODULE=$(MODULE),VERSION=$($(MODULE)_VERSION=)"
diff --git a/require.c b/require.c
index 8d36c28c5e67d7c5d69aadeea4feae10c855ab97..216ba8a4898361a82749d32456b954bc696f019e 100644
--- a/require.c
+++ b/require.c
@@ -264,13 +264,19 @@ static int runLoadScript(const char* script, const char* module, const char* ver
     return 0;
 }
 
-static void setupDbPath(const char* module, const char* dbdir)
+static int setupDbPath(const char* module, const char* dbdir)
 {
     char* old_path;           
     char* p;
     size_t len;
 
     char* absdir = realpath(dbdir, NULL); /* so we can change directory later safely */
+    if (absdir == NULL)
+    {
+        if (requireDebug)
+            printf("require: cannot resolve %s\n", dbdir);
+        return -1;
+    }
     len = strlen(absdir);
 
     if (requireDebug)
@@ -320,6 +326,7 @@ static void setupDbPath(const char* module, const char* dbdir)
                  absdir, old_path);
     }
     free(absdir);
+    return 0;
 }
 
 static void registerModule(const char* module, const char* version, const char* location)
@@ -1322,30 +1329,16 @@ loadlib:
     if (requireDebug)
         printf("require: looking for template directory\n");
     /* filename = "<dirname>/[dirlen]<module>/<version>/R<epicsRelease>/[releasediroffs]..." */
-    if (TRY_FILE(releasediroffs, TEMPLATEDIR) ||
-        TRY_FILE(releasediroffs, ".." OSI_PATH_SEPARATOR TEMPLATEDIR))
-    {
-        setupDbPath(module, filename);
-    }
-    else
+    if (!((TRY_FILE(releasediroffs, TEMPLATEDIR) ||
+        TRY_FILE(releasediroffs, ".." OSI_PATH_SEPARATOR TEMPLATEDIR)) && setupDbPath(module, filename)))
     {
+        /* if no template directory found, restore TEMPLATES to initial value */
         char *t;
         t = getenv("TEMPLATES");
         if (globalTemplates && (!t || strcmp(globalTemplates, t) != 0))
             putenvprintf("TEMPLATES=%s", globalTemplates);
     }
 
-#define SETUP_PATH(NAME, args...) \
-    if (TRY_FILE(releasediroffs, args)) \
-    { \
-        putenvprintf("%s_" #NAME "=%s", module, filename); \
-        putenvprintf(#NAME "=%s", filename); \
-    }\
-    else \
-    { \
-        putenvprintf(#NAME "=."); \
-    }
-
     if (loaded && args == NULL) return 0; /* no need to execute startup script twice if not with new arguments */
 
     /* load startup script */