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

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)

Related

Unable to import modules in node-worker-thread-pool

I am using node-worker-thread-pool package.I am unable to import certain node modules into my worker.ts, giving an error SyntaxError: Cannot use import statement outside a module. How to resolve this. Is there any other package which can help, since my task for worker thread have other import/module dependency?
import {Item} from "#package in node modules";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at MessagePort.<anonymous> (node:internal/main/worker_thread:187:24)
at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:562:20)
at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)
Emitted 'error' event on PoolWorker instance at:
at PoolWorker.[kOnErrorMessage] (node:internal/worker:289:10)
at PoolWorker.[kOnMessage] (node:internal/worker:300:37)
at MessagePort.<anonymous> (node:internal/worker:201:57)
at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:562:20)
at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)
at PoolWorker.[kOnExit] (node:internal/worker:267:5)
at Worker.<computed>.onexit (node:internal/worker:198:20)

html-pdf: Failed to load PhantomJS module. Error: Cannot find module 'phantomjs-prebuilt'

I am using html-pdf NPM module to convert my html (bill.html) code to pdf file (bill.pdf), I have simple html with heading tag and exporting that html by fs module below
import { create } from 'html-pdf';
import fs from 'fs';
import path from 'path';
var html = fs.readFileSync(path.resolve(__dirname, "./bill.html"), 'utf8');
var options = { format: 'Letter' };
create(html, options).toFile('./bill.pdf', function(err, res) {
if (err) return console.log(err);
console.log(res);
});
And I am getting this error of 'phantomjs-prebuilt'
html-pdf: Failed to load PhantomJS module. Error: Cannot find module 'phantomjs-prebuilt'
Require stack:
- /home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js
- /home/hardy/Documents/personal/api/node_modules/html-pdf/lib/index.js
- /home/hardy/Documents/personal/api/src/app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
at Function.Module._load (internal/modules/cjs/loader.js:686:27)
at Module.require (internal/modules/cjs/loader.js:848:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js:7:19)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Object.require.extensions.<computed> [as .js] (/home/hardy/Documents/personal/api/node_modules/babel-register/lib/node.js:152:7)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js',
'/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/index.js',
'/home/hardy/Documents/personal/api/src/app.js'
]
}
Debugger listening on ws://127.0.0.1:5858/508c0ea9-c495-4254-9b05-cd2fd3cd8ae3
For help, see: https://nodejs.org/en/docs/inspector
assert.js:374
throw err;
^
AssertionError [ERR_ASSERTION] [ERR_ASSERTION]: html-pdf: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath'
at new PDF (/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/pdf.js:38:3)
at createPdf (/home/hardy/Documents/personal/api/node_modules/html-pdf/lib/index.js:10:14)
at Object.<anonymous> (/home/hardy/Documents/personal/api/src/app.js:8:1)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at loader (/home/hardy/Documents/personal/api/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.<computed> [as .js] (/home/hardy/Documents/personal/api/node_modules/babel-register/lib/node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at Object.<anonymous> (/home/hardy/Documents/personal/api/node_modules/babel-cli/lib/_babel-node.js:154:22) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: undefined,
expected: true,
operator: '=='
}
Waiting for the debugger to disconnect...
As phantomjs-rebuilt is a deprecated
You can still install it forcefully and resolve the error.
Command - npm i phantomjs-prebuilt --force

Node.js ERROR Cannot find Module

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');

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!

Pass default parameter values in function react

I got this error after updating react-native and react
I think this is right syntax, but I don't know why this error is shown:
this.list = function(table, callback, wheredata = [],selection = '',extra = '') {
SyntaxError: Unexpected token =
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 Object. (/Users/apcrat/Documents/redux/delfoo_React/api/model/catalog/cuisine_route.js:7:12)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
You need to compile your code with Babel.js to use Ecmascript 6 features or if you want to work on es5, you can do like this

Categories

Resources