I'm running a file main.js using Casper.js via casperjs main.js, which uses a module ./lib/myUtils.js, which in turn uses
var utils = require('utils')
But running casperjs main.js throws the error
Error: Cannot find module 'utils'
phantomjs://bootstrap.js:289
phantomjs://bootstrap.js:254 in require
/Users/username/casper-test/lib/myUtils.js:2
/Users/username/casper-test/lib/myUtils.js:63
TypeError: 'undefined' is not a function (evaluating 'myUtils.loadCookies()')
/Users/username/casper-test/main.js:104
The functions being imported have "use strict"; in their first line.
However putting the code from myUtils.js directly into main.js avoids the error. Why is this?
I suppose you need to insert var require = patchRequire(require); at the beginning of your file ( myUtils.js ).
Related
I use AceEditor in my code. When I try to save my changes, I get this error
Uncaught Error: couldn't load module ace/theme/ or it didn't call define
at afterLoad (ace.js:18798:1)
at ace.js:3776:1
at Array.forEach (<anonymous>)
at ace.js:3775:1
at _require (ace.js:88:1)
at req (ace.js:138:1)
at afterLoad (ace.js:3771:1)
at HTMLScriptElement.s.onload.s.onreadystatechange (ace.js:3397:1)
As I understand it, to fix the error, i need to set basepath.However, adding the line this.editor.set('basePath', 'https://unpkg.com/ace-builds#1.4.6/node-modules/react-ace/src') in the ace.tsx file does not help.
The basePath is the folder where ace.js is located. for unpkg it would be
ace.config.set('basePath', "https://unpkg.com/ace-builds#1.4.6/src-noconflict")
note that the method is on config object not on editor instance.
Depending on what packager you use there may be better ways to configure this.
I'm using the matchFeature example from NodeJS but when i try to test the following error occurs in the
const cv = require('opencv4nodejs'); line from the example.
test/matchFeature.test.js
● Test suite failed to run
TypeError: Cannot convert undefined or null to object
at Function.keys ()
at Object. (node_modules/opencv4nodejs/lib/cv.js:63:8)
at Object.
(node_modules/opencv4nodejs/lib/opencv4nodejs.js:11:79)
I think is a problem when loading the module, but i cant fix it.
I'm using Jest for testing.
If i run the file with NodeJS, works without problems:
https://prnt.sc/sq41s9
And if i run test with jest, gives the error above:
https://prnt.sc/sq42mb
The images are from the file cv.js running the file and the test
EDIT: Screenshot from jest index.js (Module null)
https://prnt.sc/sq5rxl
I was able to solve it with a global setup script which loads the module.
// setup.js
module.exports = async () => {
const cv = require('opencv4nodejs');
console.log('cv.version', cv.version);
};
And in the jest Config in package.json:
"jest": {
...
"testEnvironment": "node",
"globalSetup": "./setup.js"
}
I am working in my index.js file using the firebase-admin module from Firebase, everything works great until I decide to import the apple-icloud module (https://github.com/MauriceConrad/iCloud-API) to my index.js file.
I require both like this :
var iCloud = require('apple-icloud');
var admin = require('firebase-admin');
At the line where I use :
var db = admin.database();
The program stops and throws this error :
/home/loris/HHIOT/node_modules/#firebase/util/dist/index.node.cjs.js:64
throw assertionError(message);
^
Error: Firebase Database (${JSCORE_VERSION}) INTERNAL ASSERT FAILED: Missing index implementation for fillDefaults
at assertionError (/home/loris/HHIOT/node_modules/#firebase/util/dist/index.node.cjs.js:73:12)
at Object.assert (/home/loris/HHIOT/node_modules/#firebase/util/dist/index.node.cjs.js:64:15)
at /home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3347:18
at Object.map (/home/loris/HHIOT/node_modules/#firebase/util/dist/index.node.cjs.js:896:22)
at IndexMap.addToIndexes (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3345:31)
at ChildrenNode.updateImmediateChild (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3545:46)
at ChildrenNode.updateChild (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3560:25)
at ChildrenNode.updateChild (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:3559:67)
at SnapshotHolder.updateSnapshot (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:9368:41)
at Repo.updateInfo_ (/home/loris/HHIOT/node_modules/#firebase/database/dist/index.node.cjs.js:12882:24)
I tried to import the apple-icloud module in a separate file, it does work.
It seems this error only happens when apple-icloud and firebase-admin are both required.
I find out that if I require the module after firebase has been initialised, it does work, any idea why ?
What can be the cause of this ?
Thanks.
This is the error that I'm getting-
Uncaught Error: Module name "https" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
at makeError (require.js:168)
at Object.localRequire [as require] (require.js:1436)
at requirejs (require.js:1797)
at Dataverse_API_node.js:1
I have an explore.html page which calls the script Dataverse_API_node.js, which is itself dependent on the require.js file. My Dataverse_API_node.js script is supposed to display its output data on the explore.html page. However, I face the above error when I load the explore.html page.
Note that my Dataverse_API_node.js script runs independently without errors and displays the data in the command prompt when run through the Node.js command prompt.
There are the first few lines of the Dataverse_API_node.js script-
const https = require("https");
var parseString = require('xml2js').parseString;
const demo_url = "https://demo.dataverse.org"
const actual_url = "https://demo.dataverse.org/dataverse/hbstest"
const search_uri = "/api/search"
const options = "?q=*&subtree=COSgak"
This is the part of explore.html where I'm calling the Dataverse_API_node.js script and supposed to display the data-
<p>
<script src="Dataverse_API_node.js" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js">
document.write(search_for_all("https://demo.dataverse.org", 0, 'file'));
</script>
</p>
I'm new to Javascript and would really appreciate some help with this error. Please don't send me to requirejs.org as it doesn't really help.
Thank you for your time.
Solution found. Just use browserify. Install browserify and then run the following command-
> browserify Dataverse_API_node.js > bundle.js
Browserify goes through all the 'require' in your script and wraps it up in bundle.js and helps to run the node.js modules in the browser.
Just add the following script in your .html file-
<script type=text/javascript src="scripts/bundle.js"></script>
Node.js docs about a fs module says (https://nodejs.org/api/fs.html#fs_file_system):
To get a trace to the original call site, set the NODE_DEBUG environment variable:
And an example of setting an fs env variable:
$ env NODE_DEBUG=fs node script.js
fs.js:66
throw err;
^
Error: EISDIR, read
at rethrow (fs.js:61:21)
at maybeCallback (fs.js:79:42)
at Object.fs.readFile (fs.js:153:18)
at bad (/path/to/script.js:2:17)
at Object.<anonymous> (/path/to/script.js:5:1)
<etc.>
However, if I set fs env variable upfront in command line or using process.env in the script, the variable is set but doesn't work, no trace is shown:
Upfront in command line:
$ env NODE_DEBUG=fs // set before running a script
$ node script.js // doesn't bring the trace
In the script:
'use strict';
process.env.NODE_DEBUG = 'fs'; // set it on the second line of a script
<...> // script goes here
I both cases the result is as follows, no trace, though NODE_DEBUG=fs is set:
fs.js:81
throw err; // Forgot a callback but don't know where? Use NODE_DEBUG=fs
^
Error: EISDIR, read
at Error (native)
The question is.
Why command line inline variable setting works:
$ env NODE_DEBUG=fs node script.js,
but other ways of setting it do not work?
env runs a program with a particular environment. If it isn’t passed one, it lists environment variables instead; it can’t modify its parent process’s enivronment. To set an environment variable for the current session, use export:
export NODE_DEBUG=fs
node script.js
Shells will let you omit both export and env, though:
NODE_DEBUG=fs node script.js # Like env; doesn’t create an exported NODE_DEBUG
NODE_DEBUG=fs
node script.js