-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Require lacks support for absolute paths #83
Comments
I'm not the maintainer of goja, but I've made a contribution in the past in relation to loading Windows paths so it is possible that either my solution doesn't handle all cases or that you are running in a non-Windows environment. I don't have access to a Windows VM at the moment to test it but the last time I checked it worked fine with Windows absolute paths (there is also an automated test for this case in the linked PR). The caveat though is that loading Windows paths works only when the running program operating system target is Windows ( To rule out this you can try to print But in general, I'd personally would avoid using Windows paths. If that's not possible, one workaround for "universal" absolute paths loading I use in one of my projects is to define programmatically a global goja variable that holds the path to the current working directory or to the executable (kind of similar to the Node.js vm := goja.New()
// set the current working directory or any other path as global variable
// (see https://pkg.go.dev/os#Getwd)
cwd, _ := os.Getwd()
vm.Set("__dirname", cwd)
// should work also with Windows separator too (\)
vm.RunString("require(`${__dirname}/my/local/module.js`)") |
Firstly, I appreciate your response! I can fix this absolute path issue myself! I have identified an issue while tracking the stack!
}` |
I understand that my file writing is not standardized! Because I am only doing simple debugging work and I do not rely heavily on JavaScript, only for simple scripting work. Thank you again for your reply |
require.(*RequireModule).loadModule (resolve.go:188) github.com/dop251/goja_nodejs/require
require.(*RequireModule).loadAsFile (resolve.go:90) github.com/dop251/goja_nodejs/require
require.(*RequireModule).loadAsFileOrDirectory (resolve.go:82) github.com/dop251/goja_nodejs/require
require.(*RequireModule).loadNodeModule (resolve.go:136) github.com/dop251/goja_nodejs/require
require.(*RequireModule).loadNodeModules (resolve.go:152) github.com/dop251/goja_nodejs/require
require.(*RequireModule).resolve (resolve.go:48) github.com/dop251/goja_nodejs/require
require.(*RequireModule).Require (module.go:182) github.com/dop251/goja_nodejs/require
require.(*RequireModule).require (module.go:166) github.com/dop251/goja_nodejs/require
:2
goja.(*nativeFuncObject).vmCall (func.go:563) github.com/dop251/goja
goja.call.exec (vm.go:3629) github.com/dop251/goja
:2
goja.(*vm).run (vm.go:628) github.com/dop251/goja
goja.(*vm).runTryInner (vm.go:880) github.com/dop251/goja
goja.(*vm).runTry (vm.go:866) github.com/dop251/goja
goja.(*Runtime).RunProgram (runtime.go:1472) github.com/dop251/goja
goja.(*Runtime).RunScript (runtime.go:1408) github.com/dop251/goja
goja.(*Runtime).RunString (runtime.go:1397) github.com/dop251/goja
Client.TestVerSion (Client_test.go:10) Amp/Client
testing.tRunner (testing.go:1689) testing
testing.(*T).Run.gowrap1 (testing.go:1742) testing
runtime.goexit (asm_amd64.s:1695) runtime
---以上是执行堆栈
I used an absolute path to introduce require ('D: \ \ Code \ \ node \ \ test \ dist \ bundle. js') during debugging, which resulted in incorrect parsing
When the loadModule parameter path is reached, it has changed to /node_modules/D:\Code\node\test\dist\bundle.js
The text was updated successfully, but these errors were encountered: