leafo / lapis

A web framework for Lua and OpenResty written in MoonScript
http://leafo.net/lapis/
MIT License
3.12k stars 247 forks source link

module 'app' not found #549

Closed tpimh closed 6 years ago

tpimh commented 7 years ago

I installed openresty and lapis on Alpine Linux, and it creates new project and starts server without errors:

resty:~# /usr/local/openresty/luajit/bin/lapis new --lua
wrote   nginx.conf
wrote   mime.types
wrote   app.lua
wrote   models.lua
resty:~# /usr/local/openresty/luajit/bin/lapis server
nginx: [alert] lua_code_cache is off; this will hurt performance in /root/nginx.conf.compiled:16
2017/06/14 18:01:50 [notice] 22961#22961: using the "epoll" event method
2017/06/14 18:01:50 [notice] 22961#22961: openresty/1.11.2.3
2017/06/14 18:01:50 [notice] 22961#22961: built by gcc 6.3.0 (Alpine 6.3.0) 
2017/06/14 18:01:50 [notice] 22961#22961: OS: Linux 4.9.30-0-virthardened
2017/06/14 18:01:50 [notice] 22961#22961: getrlimit(RLIMIT_NOFILE): 1024:4096
2017/06/14 18:01:50 [notice] 22961#22961: start worker processes
2017/06/14 18:01:50 [notice] 22961#22961: start worker process 22962

But if I try to access the server, it can not find app.lua (the file is actually in my current directory):

2017/06/14 18:05:01 [error] 22962#22962: *1 lua entry thread aborted: runtime error: /usr/local/openresty/luajit/share/lua/5.1/lapis/init.lua:15: module 'app' not found:
    no field package.preload['app']
    no file '/usr/local/openresty/site/lualib/app.lua'
    no file '/usr/local/openresty/site/lualib/app/init.lua'
    no file '/usr/local/openresty/lualib/app.lua'
    no file '/usr/local/openresty/lualib/app/init.lua'
    no file './app.lua'
    no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta2/app.lua'
    no file '/usr/local/share/lua/5.1/app.lua'
    no file '/usr/local/share/lua/5.1/app/init.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/app.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/app/init.lua'
    no file '/usr/local/openresty/site/lualib/app.so'
    no file '/usr/local/openresty/lualib/app.so'
    no file './app.so'
    no file '/usr/local/lib/lua/5.1/app.so'
    no file '/usr/local/openresty/luajit/lib/lua/5.1/app.so'
    no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
coroutine 0:
    [C]: in function 'require'
    /usr/local/openresty/luajit/share/lua/5.1/lapis/init.lua:15: in function 'serve'
    content_by_lua(nginx.conf.compiled:22):2: in function <content_by_lua(nginx.conf.compiled:22):1>, client: 192.168.122.1, server: , request: "GET / HTTP/1.1", host: "192.168.122.223:8080"

What may be causing this?

aiibe commented 7 years ago

I usually create new lapis app in ~/mydir on Ubuntu 16.04 and start lapis server in that dir without problem. Nginx Openresty is installed at /usr/local/openresty/nginx/sbin/nginx.

leafo commented 7 years ago

The working directory of nginx must match where you're app.lua file is, or you need to add that directory to the lua package path via the nginx configuration file.

tpimh commented 7 years ago

The file named app.lua is in the current working directory when I run lapis server, so I think the line no file './app.lua' refers to it. I tried symlinkng it to /usr/local/openresty/site/lualib/app.lua and the result was similar. When I copied app.lua to /usr/local/openresty/site/lualib/app.lua it worked! Why is it working for copied file, but not for symlink? Why doesn't it start in current directory? The ownership and permissions didn't change.

leafo commented 7 years ago

you can print the lua variable package.path to see where lua files can be loaded. Generally there is a entry in there ./?.lua when loads from the working directory of the process. By default this would be where you start the nginx process (via lapis server)

leafo commented 6 years ago

Closing this, feel free to leave a comment if you're still having an issue.

VuDn5 commented 6 years ago

@leafo i have a similar error 2018/03/16 13:44:37 [error] 22069#22069: *2 lua entry thread aborted: runtime error: content_by_lua(nginx.conf.compiled:22):2: module 'lapis' not found: no field package.preload['lapis'] no file '/usr/local/openresty/site/lualib/lapis.ljbc' no file '/usr/local/openresty/site/lualib/lapis/init.ljbc' no file '/usr/local/openresty/lualib/lapis.ljbc' no file '/usr/local/openresty/lualib/lapis/init.ljbc' no file '/usr/local/openresty/site/lualib/lapis.lua' no file '/usr/local/openresty/site/lualib/lapis/init.lua' no file '/usr/local/openresty/lualib/lapis.lua' no file '/usr/local/openresty/lualib/lapis/init.lua' no file './lapis.lua' no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/lapis.lua' no file '/usr/local/share/lua/5.1/lapis.lua' no file '/usr/local/share/lua/5.1/lapis/init.lua' no file '/usr/local/openresty/luajit/share/lua/5.1/lapis.lua' no file '/usr/local/openresty/luajit/share/lua/5.1/lapis/init.lua' no file '/usr/local/openresty/site/lualib/lapis.so' no file '/usr/local/openresty/lualib/lapis.so' no file './lapis.so' no file '/usr/local/lib/lua/5.1/lapis.so' no file '/usr/local/openresty/luajit/lib/lua/5.1/lapis.so' no file '/usr/local/lib/lua/5.1/loadall.so' stack traceback: coroutine 0: [C]: in function 'require'

find / -name lapis.ljbc and result is nothing. how can i fix it?

leafo commented 6 years ago

@VuDn5 I'm not sure why you're searching for a ljbc file, did you install lapis in a way that compiles it to luajit bytecode?

In any case, it's not finding lapis because your package.path is not pointing to where you installed it. Did you install it with luarocks?

Assuming your luarocks executable is called luarocks, you can see what the value of package.path needs to be to find modules installed with it by running luarocks path --lr-path

You'll probably have to do the same thing for the cpath, needed by lapis' dependencies. (luarocks path --lr-cpath)

You can set package.path & co. using nginx configuration directive, or using the LUA_PATH & LUA_CPATH environment variables. Run luarocks path to get the command to set the environment variables.

VuDn5 commented 6 years ago

@leafo i install with luarock `[root@localhost ~]# luarocks install lapis Warning: falling back to curl - install luasec to get native HTTPS support Installing https://luarocks.org/lapis-1.6.0-1.src.rock... Using https://luarocks.org/lapis-1.6.0-1.src.rock... switching to 'build' mode Updating manifest for /usr/lib64/luarocks/rocks lapis 1.6.0-1 is now built and installed in /usr (license: MIT)

[root@localhost ~]# lapis server Error: file doesn't exist `nginx.conf'

VuDn5 commented 6 years ago

@leafo where exactly the package.path should be pointed? ` [root@localhost lapis] # luarocks path --lr-path /root/.luarocks/share/lua/5.1/?.lua;/root/.luarocks/share/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua

[root@localhost lapis]# luarocks path export LUA_PATH='/root/.luarocks/share/lua/5.1/?.lua;/root/.luarocks/share/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;./?.lua;/usr/lib64/lua/5.1/?.lua;/usr/lib64/lua/5.1/?/init.lua' export

LUA_CPATH='/root/.luarocks/lib64/lua/5.1/?.so;/usr/lib64/lua/5.1/?.so;./?.so;/usr/lib64/lua/5.1/loadall.so'`

i just want to run example, but it doesn't simple... ^-^

leafo commented 6 years ago

@VuDn5

You can try:

eval `luarocks path`
lapis server

see luarocks path --help

VuDn5 commented 6 years ago

@leafo ` luarocks path --help

LuaRocks 2.3.0, a module deployment system for Lua

NAME /bin/luarocks path - Return the currently configured package path.

SYNOPSIS /bin/luarocks path **and** [root@localhost lapis] eval /bin/luarocks

LuaRocks 2.3.0, a module deployment system for Lua

NAME /bin/luarocks - LuaRocks main command-line interface

SYNOPSIS /bin/luarocks [--from= | --only-from=] [--to=] [VAR=VALUE]... []

GENERAL OPTIONS These apply to all commands, as appropriate:

--server=<server>      Fetch rocks/rockspecs from this server
                       (takes priority over config file)
--only-server=<server> Fetch rocks/rockspecs from this server only
                       (overrides any entries in the config file)
--only-sources=<url>   Restrict downloads to paths matching the
                       given URL.
--tree=<tree>          Which tree to operate on.
--local                Use the tree in the user's home directory.
                       To enable it, see '/bin/luarocks help path'.
--verbose              Display verbose output of commands executed.
--timeout=<seconds>    Timeout on network operations, in seconds.
                       0 means no timeout (wait forever).
                       Default is 30.

VARIABLES Variables from the "variables" table of the configuration file can be overriden with VAR=VALUE assignments.

COMMANDS build Build/compile a rock. config Query information about the LuaRocks configuration. doc Show documentation for an installed rock. download Download a specific rock file from a rocks server. help Help on commands. Type '/bin/luarocks help ' for more. install Install a rock. lint Check syntax of a rockspec. list List currently installed rocks. make Compile package in current directory using a rockspec. new_version Auto-write a rockspec for a new version of a rock. pack Create a rock, packing sources or binaries. path Return the currently configured package path. purge Remove all installed rocks from a tree. remove Uninstall a rock. search Query the LuaRocks servers. show Show information about an installed rock. unpack Unpack the contents of a rock. upload Upload a rockspec to the public rocks repository. write_rockspec Write a template for a rockspec file.

CONFIGURATION Lua version: 5.1 Configuration files: System: /etc/luarocks/config-5.1.lua (ok) User : /root/.luarocks/config-5.1.lua (not found)

Rocks trees in use: 
    /root/.luarocks ("user")
    /usr ("system")

[root@localhost lapis] # lapis server no field package.preload['lapis'] no file '/usr/local/openresty/site/lualib/lapis.ljbc' no file '/usr/local/openresty/site/lualib/lapis/init.ljbc' no file '/usr/local/openresty/lualib/lapis.ljbc' no file '/usr/local/openresty/lualib/lapis/init.ljbc' no file '/usr/local/openresty/site/lualib/lapis.lua' no file '/usr/local/openresty/site/lualib/lapis/init.lua' no file '/usr/local/openresty/lualib/lapis.lua' no file '/usr/local/openresty/lualib/lapis/init.lua' no file './lapis.lua' no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/lapis.lua' no file '/usr/local/share/lua/5.1/lapis.lua' no file '/usr/local/share/lua/5.1/lapis/init.lua' no file '/usr/local/openresty/luajit/share/lua/5.1/lapis.lua' no file '/usr/local/openresty/luajit/share/lua/5.1/lapis/init.lua' no file '/usr/local/openresty/site/lualib/lapis.so' no file '/usr/local/openresty/lualib/lapis.so' no file './lapis.so' no file '/usr/local/lib/lua/5.1/lapis.so' no file '/usr/local/openresty/luajit/lib/lua/5.1/lapis.so' no file '/usr/local/lib/lua/5.1/loadall.so' stack traceback: coroutine 0: [C]: in function 'require' ` it nothing change. thanks for your help

leafo commented 6 years ago

It's not clear that you ran the commands as I wrote them.

luarocks path prints out shell variable exports, putting them in using eval and ` will export those variables to the current shell session, setting the LUA_PATH and LUA_CPATH. Afterwards, when you start the lapis server it should be able to read them. If it's still not picking it up you might need to add the ENV directives to nginx configuration, since it's not inheriting environment variables from the shell you're in.

I mentioned luarocks path --help since it teaches you about how the path command works.

VuDn5 commented 6 years ago

i don't know setting like what? i run luarocks install lapis in /root directives , you want me change LUA_PATH and LUA_CPATH how? sorry about my questions that not smart! @leafo

ilikebricks commented 2 years ago

I still get an error nginx: [alert] lua_code_cache is off; this will hurt performance in /Users/jinglian/Desktop/yel/nginx.conf.compiled:17 2022/04/09 21:29:12 [notice] 90669#33012369: using the "kqueue" event method 2022/04/09 21:29:12 [warn] 90669#33012369: 1024 worker_connections exceed open file resource limit: 256 2022/04/09 21:29:12 [notice] 90669#33012369: openresty/1.19.9.1 2022/04/09 21:29:12 [notice] 90669#33012369: built by clang 12.0.0 (clang-1200.0.32.29) 2022/04/09 21:29:12 [notice] 90669#33012369: OS: Darwin 19.6.0 2022/04/09 21:29:12 [notice] 90669#33012369: hw.ncpu: 8 2022/04/09 21:29:12 [notice] 90669#33012369: net.inet.tcp.sendspace: 131072 2022/04/09 21:29:12 [notice] 90669#33012369: kern.ipc.somaxconn: 128 2022/04/09 21:29:12 [notice] 90669#33012369: getrlimit(RLIMIT_NOFILE): 256:9223372036854775807 2022/04/09 21:29:12 [notice] 90669#33012369: start worker processes 2022/04/09 21:29:12 [notice] 90669#33012369: start worker process 90671 2022/04/09 21:29:15 [error] 90671#33012376: *1 lua entry thread aborted: runtime error: error loading module 'lpeg' from file '/usr/local/lib/lua/5.4/lpeg.so': dlopen(/usr/local/lib/lua/5.4/lpeg.so, 6): Symbol not found: _luaL_checkversion_ Referenced from: /usr/local/lib/lua/5.4/lpeg.so Expected in: flat namespace in /usr/local/lib/lua/5.4/lpeg.so stack traceback: coroutine 0: [C]: in function 'require' content_by_lua(nginx.conf.compiled:27):3: in main chunk, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8080"