Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 6096

Exported function seemingly won't work at file level in Node.js, but works fine in blocks and elsewhere

$
0
0

In ordinary Node.js server side (an Ubuntu server), I have a file

// admin.js..function tell(msg) {    console.log(msg)}..exports.tell = tell

(it does more than just the log, but I reduced it to that to eliminate any other variable issues.)

Then, in dozens of files of the app and in 100s of places, let's say main.js, I require "admin" in the usual way ...

// main.jsconst admin = require('./admin')..app.get('/hook', (req, res) => {    admin.tell(`blah..`)..async function handleCommand(m, whomIndex) {    ..    catch (error) {        admin.tell(`blah..`)

etc etc. This works perfectly and has for years.

However today, in main.js, I simply had this:

// main.jsconst admin = require('./admin')..// anywhere in the file at top level:console.log("yo") // works fineadmin.tell(`blah.. // ERROR

and similarly in a first-level function:

// main.jsconst admin = require('./admin')..// anywhere in the file at top level:function apnsSetup() {    console.log("yo") // works fine    admin.tell(`blah..`) // ERROR

the error being simply:

/home/ubuntu/main/main.js:666admin.tell("wtf")      ^TypeError: admin.tell is not a function

Am I misunderstanding something basic about Node.js?

I carefully tested in different files, with different functions etc. and I always get the behavior.

PS: just to be clear, this has nothing to do with browser-side code. It is vanilla Node.js in a typical Express/MySQL/Node app on an AWS Ubuntu instance.


Viewing all articles
Browse latest Browse all 6096

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>