dop251 / goja_nodejs

Nodejs compatibility library for Goja
MIT License
336 stars 81 forks source link

`require('./util')` will return the "native module" that register by `goja_nodejs/util/module.go`, expect is my `util.js` file. #35

Closed zbysir closed 2 years ago

zbysir commented 2 years ago

This code will show the problem


import (
    "github.com/dop251/goja"
    "github.com/dop251/goja_nodejs/console"
    "github.com/dop251/goja_nodejs/require"
    "testing"
)

func TestRegistryUtil(t *testing.T) {
    vm := goja.New()

    require.NewRegistry().Enable(vm)
    console.Enable(vm)

    v, err := vm.RunScript("root.js", "require('./util')")
    if err != nil {
        panic(err)
    }

    t.Logf("%+v", v.Export())
}

util.js

exports.default = "1";

It will print map[format:0x13b84c0] instead of map[default: 1].

zbysir commented 2 years ago

The reason for the error is path.Clean("./util") will return "util" in https://github.com/dop251/goja_nodejs/blob/master/require/resolve.go#L15