-1

So I ask a user: What is your username with a window.prompt

//public/js/Chat.js
username = window.prompt('What is your username?)

and in my root folder

//app.js
let chatMessage = new Chat({ message: msg, sender: username });

but of course that doesn't work because its not in the same file

So to counter that i tried this:

//public/js/Chat.js
export let username = window.prompt('What is your username?')
//app.js
import { username } from './public/js/Chat.js'

But that doesn't work because it's not a module.

Misticc
  • 1
  • 1
  • 1
    Your comment says Node.js but the prompt is obviously client side is the app.js script server side? – Thomas Frank May 03 '23 at 17:47
  • you need to use AJAX to send variables from client to server. – Barmar May 03 '23 at 17:48
  • What web server are you running in your backend code? Express? – Thomas Frank May 03 '23 at 17:52
  • If you want to send both to the client, make sure to add ```"type": "module"``` to package.json to use the module. Else, you should look at a backend at an API connecting them. – Amodh May 03 '23 at 17:56
  • To sum up some of these comments, it sounds like you have a backend and a frontend. Both are written in Javascript, but they unfortunately can't just import each other and share information. To do that, you might need to write a little API and send HTTP requests to your server. – Evert May 03 '23 at 17:58

1 Answers1

-1

Add "type": "module" into package.json to use the above code as a module.

Amodh
  • 367
  • 1
  • 3
  • 16