如何导入 luarocks 本地模块

问题描述

我已经在本地使用 Lua Rocks 安装了 cjson

 luarocks install --local  lua-cjson

但我无法访问该模块。

> require('cjson')
stdin:1: module 'cjson' not found:

软件包已安装到 ~/.luarocks。以下是文件结构

.
├── bin
│   ├── json2lua
│   └── lua2json
├── lib
│   ├── lua
│   │   └── 5.4
│   │       └── cjson.so
│   └── luarocks
│       └── rocks-5.4
│           ├── lua-cjson
│           │   └── 2.1.0.6-1
│           │       ├── bin
│           │       │   ├── json2lua
│           │       │   └── lua2json
│           │       ├── lua-cjson-2.1.0.6-1.rockspec
│           │       ├── rock_manifest
│           │       └── tests
│           │           ├── agentzh.t
│           │           ├── bench.lua
│           │           ├── example1.json
│           │           ├── example2.json
│           │           ├── example3.json
│           │           ├── example4.json
│           │           ├── example5.json
│           │           ├── genutf8.pl
│           │           ├── numbers.json
│           │           ├── octets-escaped.dat
│           │           ├── README
│           │           ├── rfc-example1.json
│           │           ├── rfc-example2.json
│           │           ├── test.lua
│           │           ├── TestLua.pm
│           │           └── types.json
│           └── manifest
└── share
    └── lua
        └── 5.4
            ├── cjson
            │   └── util.lua
            ├── json2lua.lua
            └── lua2json.lua

为了让 Lua 能够找到包,应该设置哪些环境变量?

解决方法

luarocks path 打印所有应该添加的内容

➜  ~ luarocks path
export LUA_PATH='/usr/share/lua/5.4/?.lua;/usr/share/lua/5.4/?/init.lua;/usr/lib/lua/5.4/?.lua;/usr/lib/lua/5.4/?/init.lua;./?.lua;./?/init.lua;/home/s1n7ax/.luarocks/share/lua/5.4/?.lua;/home/s1n7ax/.luarocks/share/lua/5.4/?/init.lua'
export LUA_CPATH='/usr/lib/lua/5.4/?.so;/usr/lib/lua/5.4/loadall.so;./?.so;/home/s1n7ax/.luarocks/lib/lua/5.4/?.so'
export PATH='/home/s1n7ax/.luarocks/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/s1n7ax/.s1n7ax/bin:/home/s1n7ax/.yarn/bin:/home/s1n7ax/.local/bin'

它只是打印它们而不是运行它们。所以这需要运行。我在init脚本中添加了以下行以在系统启动时添加路径

eval $(luarocks path)