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

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)

Related

export default data SyntaxError: Unexpected token export during bulding on next.js using typescript

Code available here =>
https://codesandbox.io/s/sweet-mcclintock-dhczx?file=/pages/index.js
Initial error when trying to use #iconify-icons/cryptocurrency with next.js and typescript (it happens only when in typescript).
SyntaxError: Unexpected token export
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at eval (webpack-internal:///#iconify-icons/cryptocurrency/usdt:1:18)
at Object.#iconify-icons/cryptocurrency/usdt (/sandbox/.next/server/pages/index.js:409:1)
at __webpack_require__ (/sandbox/.next/server/pages/index.js:23:31)
at eval (webpack-internal:///./pages/index.js:9:92)
at Module../pages/index.js (/sandbox/.next/server/pages/index.js:398:1)
at __webpack_require__ (/sandbox/.next/server/pages/index.js:23:31)
at /sandbox/.next/server/pages/index.js:91:18
at Object.<anonymous> (/sandbox/.next/server/pages/index.js:94:10)
at Module._compile (internal/modules/cjs/loader.js:778:30)
/sandbox/node_modules/#iconify-icons/cryptocurrency/usdt.js:6
export default data;
The problem does not happen with all libraries. It seems to me that during the transpilation babel has a problem with the library syntax.
Then I've tried to transpile the lib next-transpile-modules to fix the problem using the code below.
const withTM = require("next-transpile-modules")([
"#iconify-icons/cryptocurrency"
]);
module.exports = withTM();
But a new problem has happened.
Error: next-transpile-modules - an unexpected error happened when trying to resolve "#iconify-icons/cryptocurrency"
Error: Can't resolve '#iconify-icons/cryptocurrency' in '/sandbox'
at getPackageRootDirectory (/sandbox/node_modules/next-transpile-modules/src/next-transpile-modules.js:87:11)
at Array.map (<anonymous>)
at generateModulesPaths (/sandbox/node_modules/next-transpile-modules/src/next-transpile-modules.js:99:33)
To conclude, transpiling the module is the way I'm trying to achieve the main goal. It might not be necessary.
The way the #iconify-icons/cryptocurrency library is exported means you need to transpile each icon package you use individually.
const withTM = require("next-transpile-modules")([
"#iconify-icons/cryptocurrency/usdt"
]);
module.exports = withTM();

TypeError: module.exports is not a function

My goal is to use the logger.js inside my app.js
I typed this from a tutorial but I have got different results than the tutorial.
-node-tutorial
--logger.js
--app.js
When I type node app.js in the console, I get this:
timbliefert#Tims-Air node-tutorial % node app.js
/Users/timbliefert/Developer/node-tutorial/logger.js:9
module.exports(log) = log;
^
TypeError: module.exports is not a function
at Object.<anonymous> (/Users/timbliefert/Developer/node-tutorial/logger.js:9:8)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/Users/timbliefert/Developer/node-tutorial/app.js:2:14)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)```
module.exports is an object by default and you haven't reassigned it to a function so you're not able to invoke it with parenthesis like a function. You can add properties to module.exports exactly like how you'd add them to an object in JS through assignment.
module.exports.log = log;

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)

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

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