Q:

Lua: Read file relative to module

I am trying to write a module for Luakit that takes a .js file and injects it in every page, so a simple wrapper/workaround in absence of any web extension support. My only problem is currently that I cannot seem to find out how to read a file without require (because it's not a .lua-file, I just want to read it as a string) from the directory my module is in. Apparently, lua doesn't look for it there. I don't want to specify an absolute path, because it really only depends on the location of my module.
...
 |- mymodule.lua
 \- mymodule.js

module.lua:
local f = assert(io.open("mymodule.js", "r"))
local jsbase = f:read("*all")
f:close()

-- ...

However, this results in
E [core/common/lualib]: Lua error: mymodule.js: No such file or directory

[EDIT] What I need is basically a function to use as io.open(findContainingDirPathOfCurrentModuleWithoutHardCoding()..'mymodule.js')
[EDIT] Luakit runs on Lua 5.1.

A:

Fiddling with the attempts posted, I now arrived at this solution. It is not perfect, but works alright, and should work without issue if the module name is unique (especially when there are no other .js files in the directories from package.path):
-- get name of this module
local modulename = (function (m)
  t = {}
  for e in m do
    table.insert(t, e)
  end
  return t[#t]
end)(string.gmatch(..., "[^.]+"))

local f = nil

-- find the js file named like this module from any relevant paths
for i in string.gmatch(package.path, "[^;]+") do
  -- ignore all ?/init.lua etc.
  if string.find(i, "?.lua") then
    f = io.open(i:gsub("?.lua$", modulename..".js"), "r")
    if f ~= nil then
      -- found a match!
      break
    end
  end
end

assert(f)

local jsbase = f:read("*all")
f:close()

-- ...

