0

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)
Boky
  • 11,554
  • 28
  • 93
  • 163

0 Answers0