0

I have seen slimerjs Can not resolve required module, works with phantomjs, but that one explains absolutely nothing, so I'll dare ask the question again.

I have done this:

$ npm install -g encoding
encoding@0.1.12 /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/encoding
└── iconv-lite@0.4.13

So, apparently, it should be there?

Now, I'm trying this test script, as stated in https://www.npmjs.com/package/encoding - let's call it test_modload.js:

var encoding = require('encoding');

... and I try to run it with node:

$ node test_modload.js
module.js:338
    throw err;
    ^

Error: Cannot find module 'encoding'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:286:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/path/to/test/test_modload.js:1:78)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)

Apparently, here I have to use NODE_PATH:

NODE_PATH=/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules node test_modload.js

... and this command passes fine (i.e. returns nothing, and prints no errors).

But what if we try this with casperjs? The script becomes:

var encoding = require('encoding');
var casper = require('casper').create();
casper.run();

... and if I run with:

/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/casperjs test_modload.js

... again all is fine (i.e. returns nothing, and prints no errors).

But let's try this with casperjs with slimerjs engine:

$ SLIMERJSLAUNCHER=/usr/bin/firefox46 /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/casperjs --engine=slimerjs test_modload.js 

Script Error: Module: Can not resolve "encoding" module required by main located at file:///home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/bootstrap.js
       Stack:
         -> file:///home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/bootstrap.js: 350

Eh... I guess NODE_PATH is missing?

$ NODE_PATH=/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules SLIMERJSLAUNCHER=/usr/bin/firefox46 /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/casperjs --engine=slimerjs test_modload.js 

Script Error: Module: Can not resolve "encoding" module required by main located at file:///home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/bootstrap.js
       Stack:
         -> file:///home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/bootstrap.js: 350

Damn it, I ran out of options. What do I do now, how can I get encoding to work with slimerjs? Note that the slimerjs docs state in https://docs.slimerjs.org/current/api/require.html that one should use require.paths, however, for node standalone, it would complain with:

Error: require.paths is removed. Use node_modules folders, or the NODE_PATH environment variable instead.

... while slimerjs doesn't output this error; but still, no change if I do require.paths.push('/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules'); - it still 'can not resolve "encoding" module'.

Just to make sure I have the right path, here is a listing:

$ ls /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules
casperjs  encoding  npm  utf8  zombie
$ ls /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/encoding/
lib  LICENSE  node_modules  package.json  README.md  test

... so apparently the module is there.

So, how on earth can I get slimerjs to find and use the encoding module in a script?

Community
  • 1
  • 1
sdbbs
  • 4,270
  • 5
  • 32
  • 87
  • 1
    You shouldn't globally install regular modules. Install it again using `npm i encoding` and try again. – robertklep Jun 10 '16 at 13:07
  • Many thanks, @robertklep - tried it, but unfortunately that doesn't work; maybe it is because `casperjs` or `slimerjs` take over module loading? Really can't tell... I did try `strace`, and regular `node` which works does `open("/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/encoding/package.json", O_RDONLY|O_LARGEFILE) = 9`, however `casperjs`+`slimerjs` never even attempt to open anything in `/home/USERNAME/.nvm/` before crashing... – sdbbs Jun 10 '16 at 13:17
  • 1
    Try `strace -f ...`, I can imagine that CasperJS/SlimerJS use fork to create new processes, which by default aren't traced by `strace` – robertklep Jun 10 '16 at 13:23
  • Thanks again, @robertklep - tried it; actually, I was wrong in my comment above: even without `strace -f`, the only path being loaded from is `/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin`, but it never attempts to read anything else from `/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/`; unfortunately, `strace -f` doesn't reveal new info... – sdbbs Jun 10 '16 at 13:30
  • 1
    Perhaps SlimerJS doesn't work well with `nvm` and tries to find Node modules in their "regular" location (`/usr/local/lib/node_modules`)? Perhaps worth looking if it tries to access _anything_ with the name `encoding` (if you haven't already :-) – robertklep Jun 10 '16 at 14:27
  • Indeed @robertklep - I found https://muffinresearch.co.uk/taming-slimerjs/ which explains `slimerjs` has a totally different way of loading modules; I managed to force-load `...encoding/lib/encoding.js`, but then it depends on `iconv-lite`, which has a `package.json` which `slimerjs` doesn't parse, and no singular .js file, so I guess some sort of a rewrite of `encoding.js` specially for `slimerjs` will be required.... damn it `:/` – sdbbs Jun 10 '16 at 14:30
  • I didn't take a look at what SlimerJS actually is, but it's based on Gecko instead of V8, so no wonder it's not working well with Node-specific stuff like `nvm` :-( – robertklep Jun 10 '16 at 14:33
  • See also: [Can not resolve required module encoding · Issue #497 · laurentj/slimerjs · GitHub](https://github.com/laurentj/slimerjs/issues/497) – sdbbs Jun 29 '16 at 08:49

0 Answers0