Node.js ERROR Cannot find Module - javascript

Hey guys I'm new to JS and Node.js and I'm having trouble setting up a webdriverio project using cucumber and PageObject. And every time I try to run a test this error happens:
ERROR: Cannot find module '../support/action/openWebsite'
chrome
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/axelbarford/Desktop/Oktana-training-webdriverio/src/steps/LoginStepDef/loginStepsDef.js:1:1)
at Module._compile (module.js:570:32)
at loader (/usr/local/lib/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
This is the loginStepsDef.js file where the openWebSite is being called:
import openWebsite from '../support/action/openWebsite';
import LoginPage from '../pageobject/LoginPage/LoginPage';
module.exports = function given() {
this.Given(
/^I open salesforce login page$/,
openWebsite
);
this.When(
/^I set user "([^"]*)?" and password "([^"]*)?"$/, function(arg1,arg2) {
LoginPage.open();
LoginPage.username.setValue(arg1)
LoginPage.password.setValue(arg2)
});
this.And(
/^I click the login button$/,function(){
LoginPage.open();
LoginPage.submit();
});
}
Any idea what could be happening would be great. Do you need me to show something more let me know.

Try with this :
var openWebsite = require('../../support/action/openWebsite');
var LoginPage = require('../../pageobject/LoginPage/LoginPage');

Related

#elastic/elasticsearch-mock Cannot destructure property `ConfigurationError` of 'undefined' or 'null'

I am trying to mock #elastic/elasticsearch using the lib #elastic/elasticsearch-mock.
I have used the code as per the document and tried running but getting this
Code
const { Client } = require('#elastic/elasticsearch');
const Mock = require('#elastic/elasticsearch-mock');
const mock = new Mock();
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
});
client.info(console.log)
But getting this error,
/home/app/node_modules/#elastic/elasticsearch-mock/index.js:18
ConfigurationError,
^
TypeError: Cannot destructure property `ConfigurationError` of 'undefined' or 'null'.
at Object.<anonymous> (/home/app/node_modules/#elastic/elasticsearch-mock/index.js:17:7)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Module._load (internal/modules/cjs/loader.js:724:14)
at Function.hookedLoader [as _load] (/home/app/node_modules/mockery/mockery.js:111:12)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/app/test/lib/es-helper-test.js:12:14)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Module._load (internal/modules/cjs/loader.js:724:14)
at Function.hookedLoader [as _load] (/home/app/node_modules/mockery/mockery.js:111:12)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at /home/app/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/app/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/home/app/node_modules/mocha/lib/mocha.js:485:10)
at Object.<anonymous> (/home/app/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
Link: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-testing.html
Lib issue link : https://github.com/elastic/elasticsearch-js-mock/issues/15
Versions
"#elastic/elasticsearch": "7.13.0"
"#elastic/elasticsearch-mock": "^0.3.0"
You might need to add a resolver even just to console log the client.
From the very limited docs.
const { Client } = require('#elastic/elasticsearch')
const Mock = require('#elastic/elasticsearch-mock')
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
// Add this resolver
mock.add({
method: 'GET',
path: '/'
}, () => {
return { status: 'ok' }
})
client.info(console.log)
From my understanding you will need to add every index, query, resolver, etc that you want to mock on the client since there is no database to query against - meaning you provide the data and tell the mock how you want it returned (also still learning this and the documentation for the mock library is quite brief)

TypeError: createDebug.load is not a function

I am facing the following error while trying to serve my project built using AdonisJs.
Error:
at setup (D:\NodeJsProjects\AdonisWallet\node_modules\knex\node_modules\debug\src\common.js:261:33)
at Object.<anonymous> (D:\NodeJsProjects\AdonisWallet\node_modules\knex\node_modules\debug\src\node.js:236:37)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (D:\NodeJsProjects\AdonisWallet\node_modules\knex\node_modules\debug\src\index.js:9:19)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
This problem is caused by the following line of code. In this code, AddonModel is a lucid Model provided by adonisjs
const ad= await AddonModule.findBy('name', addon_name)
I am not sure why is this happening. I have properly imported model using const AddonModule = use('App/Models/Addon')
Code seems like this
await files.forEach(async (file) => {
var addon_name=file
const ad= await AddonModule.findBy('name', addon_name)
})

How to run javascript function from .js file via console?

I want to run some function inside javascript file via the console.
I'm using Node v12.14.1.
My case is as follows:
I have a testIt.js file as follows:
import {settings} from "./config";
const someMainFunction = (testParam) => {
console.log("This is init");
console.log("This PARAM", testParam);
otherFunction();
};
const otherFunction = () => {
console.log("This is other function")
};
export { someMainFunction }
I want to run someMainFunction via console.
I tried with
node -e 'require("./testIt").someMainFunction("Testing...")'
But it doesn't work.
I do not want to use any external libraries.
Any idea how to do that?
UPDATE
I get an error as follows:
import {settings} from "./frontend/config";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:895:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at [eval]:1:1
at Script.runInThisContext (vm.js:116:20)
at Object.runInThisContext (vm.js:306:38)
at Object.<anonymous> ([eval]-wrapper:9:26)
I've tried also with:
node --harmony testIt.js
But I get the same error.
UPDATE 2
When I try with
node --experimental-modules testIt.mjs
I get an error as follows:
(node:99449) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:84
let url = moduleWrapResolve(specifier, parentURL);
^
Error: Cannot find module /Users/username/Projects/config imported from /Users/username/Projects/testIt.mjs
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:84:13)
at Loader.resolve (internal/modules/esm/loader.js:73:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:147:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:41:40)
at link (internal/modules/esm/module_job.js:40:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
UPDATE 3
import {settings} from "./frontend/config";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:895:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at [eval]:1:1
at Script.runInThisContext (vm.js:116:20)
at Object.runInThisContext (vm.js:306:38)
at Object.<anonymous> ([eval]-wrapper:9:26)

"TypeError: jsdom.jsdom is not a function" with paper-node.js in npm paper module

I have installed webppl-agents library (along with webppl and webppl-dp) and am trying to run the command line test, but I'm running into some trouble. It appears there is a dependency issue with jsdom from the npm paper module (1) (2) (3) (4), but I haven't been able to get a complete handle on the issue at this point.
Does anybody know what's going on here? Do I just need to use a newer version of paper?
PS C:\Users\user\.webppl\node_modules\webppl-agents> webppl --require webppl-dp --require . tests/tests.wppl
C:\Users\user\.webppl\node_modules\webppl-agents\node_modules\paper\dist\paper-node.js:10835
document = jsdom.jsdom('<html><body></body></html>'),
^
TypeError: jsdom.jsdom is not a function
at new <anonymous> (C:\Users\user\.webppl\node_modules\webppl-agents\node_modules\paper\dist\paper-node.js:10835:19)
at Object.<anonymous> (C:\Users\user\.webppl\node_modules\webppl-agents\node_modules\paper\dist\paper-node.js:33:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\user\.webppl\node_modules\webppl-agents\src\visualization\gridworld.js:1:75)
There has been some breaking changes recently. The following way of initializing jsodm, document and window fixed it for me:
import jsdom from 'jsdom';
const {JSDOM} = jsdom;
const {document} = (new JSDOM('<!doctype html><html><body></body></html>')).window;
global.document = document;
global.window = document.defaultView;

I cannot use import in Jasmine2 spec when using protractor 4.0.9 when using typescript 2.0

When I try to use typescript .ts spec and run them in protractor, I get:
[17:05:52] E/launcher - Error: SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at C:\Users\gwk736\Gitlab\inform-locate-E2E-tests\node_modules\jasmine\lib\jasmine.js:84:5
at Array.forEach (native)
at Jasmine.loadSpecs (C:\Users\gwk736\Gitlab\inform-locate-E2E-tests\node_modules\jasmine\lib\jasmine.js:83:18)
[17:05:52] E/launcher - Process exited with error code 100
When I have .ts file and no import is present it's behaving ok. Why ?
) Get the same error, using: Jasmine2, Protractor 4.0.11, typescript 2.0.3
And I don't know what seems to be the problem. But my workaround is:
in file: main-page.ts
module.exports = {
setUrl: function(){
browser.get("/");
}
};
and in file you want to use the function setURL for example:
var mainPage = require('./main-page.ts');
describe('Test Main Page', function() {
it('should have a title', function() {
mainPage.setUrl();
expect(browser.getTitle()).toEqual('Titel meiner Seite');
});
});
That's just my workaround - actually I didn't find out the real reason for the problem you described!

Categories

Resources