0

I am new to Lua. I compiled a JS file to lua 5.2 using castl:

castl -o script.js

This created a file named script.js.lua. This is the first line of my new lua file:

local _ENV = require("castl.runtime");

I tried running that file with lua52 script.js.lua, but I got this error:

C:\Program Files\lua-5.2.4_Win64_bin\lua52.exe: script.js.lua:1: module 'castl.runtime' not found:
        no field package.preload['castl.runtime']
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\lua\castl\runtime.lua'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\lua\castl\runtime\init.lua'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime.lua'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime\init.lua'
        no file '.\castl\runtime.lua'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\loadall.dll'
        no file '.\castl\runtime.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl\runtime52.dll'
        no file '.\castl\runtime52.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\loadall.dll'
        no file '.\castl.dll'
        no file 'C:\Program Files\lua-5.2.4_Win64_bin\castl52.dll'
        no file '.\castl52.dll'
stack traceback:
        [C]: in function 'require'
        script.js.lua:1: in main chunk
        [C]: in ?

What am I missing? I am on Windows 10.

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
  • From the docs: a Lua runtime library (located in `lua/castl/`) which allows the execution of the code compiled by `castl.js`. Add the same to your environment's LUA_PATH, and it should work. – hjpotter92 Mar 20 '17 at 08:41

2 Answers2

-1

To answer your question

What am I missing?

Your problem is that you use a function require, but you don't know what this function does. Therefore, you cannot understand the errors it throws when it fails to do what you're expecting it to do.

So first refer to the Lua manual: https://www.lua.org/manual/5.3/manual.html#pdf-require

There you'll find:

require searches for a Lua loader using the path stored in package.path. If that also fails, it searches for a C loader using the path stored in package.cpath. If that also fails, it tries an all-in-one loader (see package.searchers).

and https://www.lua.org/manual/5.3/manual.html#pdf-package.path

The path used by require to search for a Lua loader.

At start-up, Lua initializes this variable with the value of the environment variable LUA_PATH_5_3 or the environment variable LUA_PATH or with a default path defined in luaconf.h, if those environment variables are not defined. Any ;; in the value of the environment variable is replaced by the default path.

For additional reading: https://www.lua.org/pil/8.1.html

Entering "Lua require" into any web search will also result in plenty of solutions to your problem.

As hjpotter92 already stated in his comment, you have to tell your computer where to look for the files you want to require unless they are already located in the default folders. Add the file's location to your LUA_PATH environment variable or add it to the package.path string before calling the respective require.

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
Piglet
  • 27,501
  • 3
  • 20
  • 43
  • I probably just didn't understand your instructions correctly, but I tried adding a new user variable in the environment variables dialogue named `LUA_PATH` set to `%USERPROFILE%\Desktop\repos\clones\castl\lua\castl`. After this I was getting the same error except the error only listed "no file" entries for `.dll` files (no `.lua` files anymore). – Web_Designer Mar 20 '17 at 19:26
  • So, noticing that without that environment variable Lua was searching for a `runtime.lua` file in ``C:\Program Files\ua-5.2.4_Win64_bin\lua\castl\``, I copied the `lua` folder in my castl repo into that location (``C:\Program Files\lua-5.2.4_Win64_bin\``). This seems to have moved me past this error on to another... – Web_Designer Mar 20 '17 at 20:29
  • I'm sorry but "Entering "Lua require" into any web search" is not a good advice for answering question at Stackoverflow - answers here supposed to be self-sufficient to some extent. – shabunc Jun 29 '19 at 11:42
  • @shabunc my answer is self-sufficient. you obviously didn't read it. I explained in detail what causes his problem and how he can solve it. I even named several references. I even explained how to get that information without being spoon-fed on StackOverflow. – Piglet Jun 30 '19 at 13:48
-1

I'll leave an answer here since I believe that answer provided is actually not helping to someone who does not know anything about Lua ecosystem.

In order to require("castl.runtime") to be resolved one need to set LUA_PATH, in this case, for the sake of simplicity let's assume that castl distrib is located at /Users/me/castl, then you can:

export LUA_PATH="/Users/me/castl/lua/?.lua"

Now if you'll run Lua script it will be executed properly.

shabunc
  • 23,119
  • 19
  • 77
  • 102