slimerjs Can not resolve required module encoding

2019-08-17 16:38发布

问题:

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?