How to export variable from node js(commonjs) to normal javascript( vanilla js)?

Viewed 450

i am creating an express project where i have some variables which needed to be passed in raw javascript(placed in folder static)

my express project name app.js my raw javascript file index.js

lets say i want to export variable x = 10;

i did

//app.js
var fs  = require("fs");
//my express code 
const x = 10;     
module.exports = x;

now my normal javascript file

  //index.js
  import "../app.js"
  window.alert(x)// how to alert x in window

my views html

 <script src=./static/index.js ></script>

// please also explain any turaround if any //i read all the questions but didnt find one answering my question in simple terms

how to do it?

1 Answers

Anything placed inside the static folder can be accessed by your front end code by simply going to /

For example, if the static folder set by you is named static and your JS file which you want to reference is /static/index.js, you need to write the script tag as <script src="/index.js"></script>.

You don't need to write the name of the static folder. By default, the / is going to look into the static folder.

Related