\RequirePackage{iftex}
\RequireLuaTeX
\documentclass[12pt]{article}
\begin{filecontents}[overwrite,noheader]{typeout2.lua}
function foo()
return "FOO"
end
return {foo=foo}
\end{filecontents}
\directlua{typeout2 = require "typeout2"}
\NewExpandableDocumentCommand{\foo}{}
{
\directlua{typeout2.foo()}
}
\begin{document}
\typeout{FUNCTION FOO IS \foo}
\foo
\end{document}