saghul / sjs

Skookum JS: a JavaScript runtime
http://sjs.saghul.net
MIT License
98 stars 13 forks source link

require, scripts, module not found #56

Closed animetronix closed 7 years ago

animetronix commented 7 years ago

Best to explain with an example:

$ cat foo.js
module.exports = "bar";
$ sjs
Skookum JS 0.4.0 running on osx x64
[Engine: Duktape v1.5.0 (83d5577)]
[Build: Debug on 2017-01-02T18:01:47Z]
[Clang 7.3.0.7030031 on Darwin-15.6.0]
sjs> require("./foo")
= "bar"
sjs>
$ cat bar.js
console.log(require("./foo"));
$ sjs bar.js
Error: Module not found
    at [anon] (.../sjs/src/modules.c:222) internal
    at [anon] () native strict preventsyield
    at require () native strict preventsyield
    at main (.../bar.js:1) preventsyield
saghul commented 7 years ago

Hum, does sjs ./bar.js work? At any rate, I plan on updating the Duktape engine to v2 soon, I'll also take care of this before the release. Thanks for the report!

animetronix commented 7 years ago

@saghul clobbered and rebuilt sjs, this specific error went away but a related one showed up:

$ cat foo.js
module.exports = "bar";
$ cat bar.js
console.log(require("./foo"));
$ sjs bar.js
bar
$ sjs -e 'require("./foo.js")'
Error: Module not found
...

It seems like the module resolution process from the REPL differs from the module resolution process for a script invocation and the process for the eval mode.

saghul commented 7 years ago

Looks like this got fixed somewhere along the line, I can no longer reproduce it.

fbukevin commented 6 years ago

I had the same problem like @animetronix.

Please look into Makefile and you will see that the runnable REPL command is make run. It indicates that you should export the environment variable SJS_PATH=build:modules so that no matter you get into REPL with ./build/sjs (not make run) or run script file as $ ./build/sjs foo.js, all module could be found.