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?