Using function from module in JavaScript script - javascript

I want to use an exported constant from a JS module. My simple script is like
#!/usr/bin/env node
const pi = require('./custom_math').pi
console.log(pi)
But the module I require has an import, so It raises the following error:
$ ./script.js
<path_to_module>/index.js:1
import anotherconstant from '<other_module>'
^^^^^^
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 Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (./script.js:3:12)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
How can I make a script that uses the pi constant from the simple_math/index.js file?

Related

Getting started with Mongoose

When I put in the code
const mongoose = require("mongoose")
I get back the following error:
/Users/user/shares3/node_modules/mongodb/lib/utils.js:1069
catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/user/shares3/node_modules/mongodb/lib/operations/add_user.js:6:17)
This code is as simple as could be. I have tried reinstalling the mongoose package.
Do you have any ideas on how I can troubleshoot this?
The issue is caused by using a (very) old version of Node.js that doesn't support try/catch.

Can't import module from ES 6 module which requires

My module has type set as module in package.json, hence it is ES 6.
I need to import a module (which I do using import), but this module (dexie-encrypted) does require to a third module, therefor failing:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/xxx/xxx/node_modules/dexie/dist/modern/dexie.mjs
at new NodeError (node:internal/errors:329:5)
at Module.load (node:internal/modules/cjs/loader:970:11)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (/Users/xxx/xxx/node_modules/dexie-encrypted/dist/applyMiddleware.js:5:41)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14) {
code: 'ERR_REQUIRE_ESM'
}
How is a situation like this solved?

Can I use package.json "resolutions" to substitute in lodash-es for lodash?

Pretty straightforward, I'm wondering if resolutions can be used in this fashion (substituting in a different) package like you can for dependencies.
"resolutions": {
"lodash": "npm:lodash-es#^14.7.x"
},
And if I can do that with NPM or Yarn, are there problems beyond the normal risks if I were to force lodash to a certain version.
Maybe it can work, but I couldn't make it work with Webpack's DLL generation.
C:\projects\myProject\node_modules\lodash\lodash.js:10
export { default as add } from './add.js';
^^^^^^
SyntaxError: Unexpected token 'export'
at Module._compile (internal/modules/cjs/loader.js:892:18)
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 Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:\projects\myProject\node_modules\concurrently\src\flow-control\kill-others.js:1:11)
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)
error Command failed with exit code 1.

An error while trying to build bundle with webpack

I`ve just started to learn nodejs and react and I am trying to build bundle using webpack and I receive next error. How can I make it work?
C:\Users\Admin\WebstormProjects\reactandwebpacktests>webpack
C:\Users\Admin\WebstormProjects\reactandwebpacktests\node_modules\v8-compile-cache\v8-compile-cache.js:161
return mod.npm require(id);
^^^^^^^
SyntaxError: Unexpected identifier
?[90m at wrapSafe (internal/modules/cjs/loader.js:1054:16)?[39m
?[90m at Module._compile (internal/modules/cjs/loader.js:1102:27)?[39m
?[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)?[39m
?[90m at Module.load (internal/modules/cjs/loader.js:986:32)?[39m
?[90m at Function.Module._load (internal/modules/cjs/loader.js:879:14)?[39m
?[90m at Module.require (internal/modules/cjs/loader.js:1026:19)?[39m

Resolving template string interpolation at run-time, not at bundle creation time

I have a JavaScript application that is bundled into a client-side application and run in the browser.
An old version of the application used to have a variable assignment like this:
const API_ROOT_URL = `http://${window.location.hostname}:8000/`;
This would presumably get interpolated in the browser.
However, this code now throws a webpack error on webpack 4.42.0:
ReferenceError: window is not defined
at Object.<anonymous> (/usr/src/app/src/client_config.js:4:32)
at Module._compile (/usr/src/app/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (/usr/src/app/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
at Object.<anonymous> (/usr/src/app/webpack.config.js:1:99)
at Module._compile (/usr/src/app/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (/usr/src/app/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
at WEBPACK_OPTIONS (/usr/src/app/node_modules/webpack-cli/bin/utils/convert-argv.js:114:13)
at requireConfig (/usr/src/app/node_modules/webpack-cli/bin/utils/convert-argv.js:116:6)
at /usr/src/app/node_modules/webpack-cli/bin/utils/convert-argv.js:123:17
at Array.forEach (<anonymous>)
at module.exports (/usr/src/app/node_modules/webpack-cli/bin/utils/convert-argv.js:121:15)
at /usr/src/app/node_modules/webpack-cli/bin/cli.js:71:45
at Object.parse (/usr/src/app/node_modules/yargs/yargs.js:567:18)
at /usr/src/app/node_modules/webpack-cli/bin/cli.js:49:8
at Object.<anonymous> (/usr/src/app/node_modules/webpack-cli/bin/cli.js:366:3)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/usr/src/app/node_modules/webpack/bin/webpack.js:156:2)
It seems that the interpolation is happening at the time the bundle is created, when window doesn't exist.
Can I get the intended behavior?

Categories

Resources