others-how to solve the `module not found` error when running a lua script in openresty?
1. Problem
Here is the error message:
/usr/local/openresty/nginx/sbin/nginx -c /Users/bswen/lua-projects/openresty-demo/conf/nginx.conf -g daemon off; pid '/Users/bswen/lua-projects/openresty-demo/nginx.pid';
2022/06/01 14:28:41 [error] 17956#0: *1 lua entry thread aborted: runtime error: content_by_lua(nginx-listen-30086.conf:44):3: module 'resty.jwt' not found:
no field package.preload['resty.jwt']
no file '/Users/bswen/lua-projects/openresty-demo/src/resty/jwt.lua'
no file '/Users/bswen/lua-projects/lua-resty-jwt/lib/resty/resty/jwt.lua'
no file '/usr/local/openresty/site/lualib/resty/jwt.ljbc'
no file '/usr/local/openresty/site/lualib/resty/jwt/init.ljbc'
no file '/usr/local/openresty/lualib/resty/jwt.ljbc'
no file '/usr/local/openresty/lualib/resty/jwt/init.ljbc'
no file '/usr/local/openresty/site/lualib/resty/jwt.lua'
no file '/usr/local/openresty/site/lualib/resty/jwt/init.lua'
no file '/usr/local/openresty/lualib/resty/jwt.lua'
no file '/usr/local/openresty/lualib/resty/jwt/init.lua'
no file './resty/jwt.lua'
no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/resty/jwt.lua'
no file '/usr/local/share/lua/5.1/resty/jwt.lua'
no file '/usr/local/share/lua/5.1/resty/jwt/init.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/resty/jwt.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/resty/jwt/init.lua'
no file '/usr/local/openresty/site/lualib/resty/jwt.so'
no file '/usr/local/openresty/lualib/resty/jwt.so'
no file './resty/jwt.so'
no file '/usr/local/lib/lua/5.1/resty/jwt.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty/jwt.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file '/usr/local/openresty/site/lualib/resty.so'
no file '/usr/local/openresty/lualib/resty.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
coroutine 0:
[C]: in function 'require'
content_by_lua(nginx-listen-30086.conf:44):3: in main chunk, client: ::1, server: _, request: "GET /sign HTTP/1.1", host: "localhost:30086"
::1 - - [01/Jun/2022:14:28:41 +0800] "GET /sign HTTP/1.1" 500 183 "-" "curl/7.64.1"
The key error message is:
/usr/local/openresty/nginx/sbin/nginx -c /Users/bswen/lua-projects/openresty-demo/conf/nginx.conf -g daemon off; pid '/Users/bswen/lua-projects/openresty-demo/nginx.pid';
2022/06/01 14:28:41 [error] 17956#0: *1 lua entry thread aborted: runtime error: content_by_lua(nginx-listen-30086.conf:44):3: module 'resty.jwt' not found:
no field package.preload['resty.jwt']
no file '/Users/bswen/lua-projects/openresty-demo/src/resty/jwt.lua'
no file '/Users/bswen/lua-projects/lua-resty-jwt/lib/resty/resty/jwt.lua'
no file '/usr/local/openresty/site/lualib/resty/jwt.ljbc'
The code that cause the error is as follows:
nginx.conf
:
lua_package_path '/Users/bswen/lua-projects/lua-resty-jwt/lib/resty/?.lua;;';
directory structure:
➜ resty git:(master) pwd
/Users/bswen/lua-projects/lua-resty-jwt/lib/resty
➜ resty git:(master) ll
total 112
-rw-r--r-- 1 bswen staff 11K Jun 1 14:24 evp.lua
-rw-r--r-- 1 bswen staff 15K Jun 1 14:24 jwt-validators.lua
-rw-r--r-- 1 bswen staff 27K Jun 1 14:24 jwt.lua
➜ resty git:(master)
lua code:
location = /sign {
content_by_lua '
local cjson = require "cjson"
local jwt = require "resty.jwt"
local jwt_token = jwt:sign(
"lua-resty-jwt",
{
header={typ="JWT", alg="HS256"},
payload={foo="bar"}
}
)
ngx.say(jwt_token)
';
}
2. Reason
Openresty try to find the lua library from the lua_package_path
, if the lua_package_path
is as follows:
lua_package_path '/Users/bswen/lua-projects/lua-resty-jwt/lib/resty/?.lua;;';
if you import library as follows:
local jwt = require "resty.jwt"
openresty will try to find the library from this path:
/Users/bswen/lua-projects/lua-resty-jwt/lib/resty/resty/jwt.lua
You can see that the above path does NOT match our directory structure. That is the problem.
What is lua_package_path
?
syntax: lua_package_path
default: The content of LUA_PATH environment variable or Lua’s compiled-in defaults.
context: http
Sets the Lua module search path used by scripts specified by set_by_lua, content_by_lua and others. The path string is in standard Lua path form, and ;; can be used to stand for the original search paths.
# Set the search path for pure Lua extension libraries (';;' is the default path):
lua_package_path '/foo/bar/?.lua;/blah/?.lua;;';
# Set the search path for Lua extension modules written in C (can also use ';;'):
lua_package_cpath '/bar/baz/?.so;/blah/blah/?.so;;';
3. Solution
change nginx.conf’s lua_package_path from
lua_package_path '/Users/bswen/lua-projects/lua-resty-jwt/lib/resty/?.lua;;';
to
lua_package_path '/Users/bswen/lua-projects/lua-resty-jwt/lib/?.lua;;';
4. Summary
In this post, I demonstrated how to solve the module not found
error when running a lua script in openresty, the key point is to set the correct lua_package_path
that matches your directory structure . That’s it, thanks for your reading.