Skip to content
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

commonjs require on windows #53

Closed
jgranduel opened this issue Jun 22, 2012 · 4 comments
Closed

commonjs require on windows #53

jgranduel opened this issue Jun 22, 2012 · 4 comments
Labels
CommonJS Issues related to the CommonJS implementation in Rhino

Comments

@jgranduel
Copy link

Hi,

rhino-1.7R4.
I'm experiencing difficulties to load commonjs modules on windows (win 7, powershell env). Please, precise how to properly load commonjs modules if behaviour is not fully standard. I'm just testing with underscore for using a popular module.
Thanks,

ex:
PS D:\temp>mkdir test
PS D:\temp>cd test
PS D:\temp>mkdir node_modules
PS D:\temp>npm install underscore
PS D:\temp\test> java org.mozilla.javascript.tools.shell.Main -require -modules ./node_modules
Rhino 1.7 release 4 2012 06 18
js> require("underscore")
js: "file:/D:/temp/test/node_modules/underscore", line 1: erreur de syntaxe
js: .npmignore
js: .^
js: "file:/D:/temp/test/node_modules/underscore", line 1: La compilation a produit 1 erreur(s) de syntaxe.

removing .npmignore
PS D:\temp\test> rm \node_modules\underscore.npmignore
PS D:\temp\test\node_modules> java org.mozilla.javascript.tools.shell.Main -require -modules ./node_modules
js> require("underscore")
js: uncaught JavaScript runtime exception: ReferenceError: "CNAME" is not defined

removing all folders
PS D:\temp\test\node_modules> java org.mozilla.javascript.tools.shell.Main -require -modules ./node_modules
js> require("underscore")
js: uncaught JavaScript runtime exception: ReferenceError: "favicon" is not defined

PS D:\temp\test\node_modules> ls .\underscore
package.json
README.md
underscore-min.js
underscore.js

PS D:\temp\test\node_modules> java org.mozilla.javascript.tools.shell.Main -require -modules ./node_modules
js> require("underscore")
js: uncaught JavaScript runtime exception: ReferenceError: "README" is not defined

PS D:\temp\test\node_modules> java org.mozilla.javascript.tools.shell.Main -require -modules ./node_modules
js> require("underscore")
js: uncaught JavaScript runtime exception: ReferenceError: "underscore" is not defined

js> require("underscore.js")
js: exception from uncaught JavaScript throw: Error: Module "underscore.js" not found.

in fact cannot load it!

Also, couldn't modules full Path be prefixed with "/" automatically on windows?

PS D:\temp> java org.mozilla.javascript.tools.shell.Main -require -modules /D:/Javascript/Code
Rhino 1.7 release 4 2012 06 18
js> require("time")
[object Object]
js> quit()
PS D:\temp> java org.mozilla.javascript.tools.shell.Main -require -modules D:/path/to/modules
Rhino 1.7 release 4 2012 06 18
js> require("aModule")
org.mozilla.javascript.WrappedException: Wrapped java.net.MalformedURLException: unknown protocol: d
at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1754)
at org.mozilla.javascript.commonjs.module.Require.getModule(Require.java:375)
at org.mozilla.javascript.commonjs.module.Require.getExportedModuleInterface(Require.java:264)
at org.mozilla.javascript.commonjs.module.Require.call(Require.java:218)
at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:63)
at org.mozilla.javascript.gen._stdin__1._c_script_0(Unknown Source)
at org.mozilla.javascript.gen._stdin__1.call(Unknown Source)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
at org.mozilla.javascript.gen._stdin__1.call(Unknown Source)
at org.mozilla.javascript.gen._stdin__1.exec(Unknown Source)
at org.mozilla.javascript.tools.shell.Main.processSource(Main.java:502)
at org.mozilla.javascript.tools.shell.Main.processFiles(Main.java:178)
at org.mozilla.javascript.tools.shell.Main$IProxy.run(Main.java:102)
at org.mozilla.javascript.Context.call(Context.java:489)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:504)
at org.mozilla.javascript.tools.shell.Main.exec(Main.java:160)
at org.mozilla.javascript.tools.shell.Main.main(Main.java:138)
Caused by: java.net.MalformedURLException: unknown protocol: d
at java.net.URL.(URL.java:590)
at java.net.URL.(URL.java:480)
at java.net.URL.(URL.java:429)
at java.net.URI.toURL(URI.java:1096)
at org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider.loadFromActualUri(UrlModuleSourceProvider.java:137)
at org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider.loadFromUri(UrlModuleSourceProvider.java:127)
at org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider.loadFromPathList(UrlModuleSourceProvider.java:112)
at org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider.loadFromPrivilegedLocations(UrlModuleSourceProvider.java:93)
at org.mozilla.javascript.commonjs.module.provider.ModuleSourceProviderBase.loadSource(ModuleSourceProviderBase.java:41)
at org.mozilla.javascript.commonjs.module.provider.CachingModuleScriptProviderBase.getModuleScript(CachingModuleScriptProviderBase.java:69)
at org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider.getModuleScript(SoftCachingModuleScriptProvider.java:67)
at org.mozilla.javascript.commonjs.module.Require.getModule(Require.java:363)
... 16 more

@hns
Copy link
Contributor

hns commented Jul 2, 2012

As far as I can see this shows two different problems with Rhino's CommonJS module loader:

  • loading packages/directories through a package.json file is not supported. It seems like calling require with an id that represents a directory tries to parse the directory listing as module, which is completely bogus.
  • Windows paths (starting with a drive letter followed by a colon) are interpreted as URLs by the shell.

@jgranduel
Copy link
Author

Hi,

thank you for checking this issue. I feel it important for node.js compatibility for at least importing pure JS module. As far as I can see , the mentioned https://github.com/micmath/Rhino-Require was complete (except the C: D: windows error I changed for myself). Rhino.Require details checks all ways to load commonjs that you might also have in ringo.js.

@anba
Copy link
Contributor

anba commented Jul 12, 2012

The Windows paths bug is also reported at #10

@p-bakker p-bakker added the CommonJS Issues related to the CommonJS implementation in Rhino label Jun 29, 2021
@p-bakker
Copy link
Collaborator

p-bakker commented Feb 9, 2023

Am closing this as the Windows path issue is not an issue at all, see #10 and the desired support for loading (nodejs) packages is already requested in #55, making this issue a duplicate

@p-bakker p-bakker closed this as completed Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CommonJS Issues related to the CommonJS implementation in Rhino
Projects
None yet
Development

No branches or pull requests

4 participants