Problem when I try to import my own modules into Lua (not found)

Asked

Viewed 47 times

1

In the main file.lua:

local calculadora = {
  somar = function(x, y)
    return x + y
  end
}

function calculadora.multiplicar(x, y)
  return x * y
end

return calculadora

In the Copyofmain file.

local calculadora = require("calculadora")

print(calculadora.somar(1, 1))

captura de tela do projeto

error:

Exception in thread "main" com.naef.jnlua.LuaRuntimeException: ..._64\workspace\criando bilbiotecas\src\CopyOfmain.lua:1: module 'calculadora' not found:
    no field package.preload['calculadora']
    no file '.\calculadora.lua'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\lua\calculadora.lua'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\lua\calculadora\init.lua'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\calculadora.lua'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\calculadora\init.lua'
    no file 'C:\Users\lidio\Downloads\org.eclipse.ldt.product-win32.win32.x86_64\workspace\criando bilbiotecas\src\calculadora.lua'
    no file 'C:\Users\lidio\Downloads\org.eclipse.ldt.product-win32.win32.x86_64\workspace\criando bilbiotecas\src\calculadora\init.lua'
    no file 'C:\Users\lidio\Downloads\org.eclipse.ldt.product-win32.win32.x86_64\workspace\criando bilbiotecas\src\calculadora.luac'
    no file 'C:\Users\lidio\Downloads\org.eclipse.ldt.product-win32.win32.x86_64\workspace\criando bilbiotecas\src\calculadora\init.luac'
    no file '.\calculadora.dll'
    no file '.\calculadora51.dll'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\calculadora.dll'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\calculadora51.dll'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\clibs\calculadora.dll'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\clibs\calculadora51.dll'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\loadall.dll'
    no file 'C:\Program Files\Java\jre1.8.0_251\bin\clibs\loadall.dll'
    at com.naef.jnlua.LuaState.lua_pcall(Native Method)
    at com.naef.jnlua.LuaState.call(LuaState.java:555)
    at org.eclipse.ldt.support.lua51.internal.interpreter.JNLua51Launcher.run(JNLua51Launcher.java:128)
    at org.eclipse.ldt.support.lua51.internal.interpreter.JNLua51Launcher.main(JNLua51Launcher.java:143)

I put the script on Repl.it. There is also giving error

  • I tested here and to get it to work I renamed the file CopyOfmain.lua for calculadora.lua

  • @Code for me keeps giving error, I put the script on the site repl it. and beyond the error

  • @Jeffersonquesado Now I get it. I tested now and it worked! Thank you

  • I put as response the comment, @Gustavogld

2 answers

3

For documentation, you pass require("name-of-file").

By calling require "lili", it will search for the following files:

  • lili
  • lili.lua
  • c:\windows\lili
  • /usr/local/lua/lili/lili.lua

In his example, the require is called passing as "calculator" argument, but has no file called calculadora, calculadora.lua or anything of the kind.

1

Error because you tried to require the function instead of the module, you should do as below:

local Module = require("CopyOfmain")
local calculadora = Module.calculadora

print(calculadora.somar(1, 1))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.