0

I have a typescript file of a module I created

export function hello() {
 console.log('hello');
}
export function bye() {
 console.log('bye');
}

in my html I have actions like such:

 <head>
 <script src="./assets/js/myModule.ts" type="module"></script>
 </head>
 <body>
 <span onclick="myModule.hello()">Press to greet</span>
 </body>
...

The app has a server.js file:

const proxy = require("http-proxy-middleware").createProxyMiddleware;
const Bundler = require("parcel-bundler");
const express = require("express");
const bundler = new Bundler("index.html");
const app = express();
app.use(
 "/api",
 proxy({
 target: process.env.API_SERVER || "http://localhost:1337/"
 })
);
app.use(bundler.middleware());
app.listen(Number(process.env.PORT || 1234));

But everytime I launch the server, I get an error: myModule is not defined

I tried already:

  • Using npm link.
  • requiring the file in the server.js and passing it with app.use(myModule.ts)
asked May 31, 2021 at 17:28
1
  • JS modules don't expose their exports to the global (window) namespace, so you can't call them from an HTML attribute without more setup. This question and its two answers explain further. Commented May 31, 2021 at 19:06

1 Answer 1

1

try define the static file, something like:

app.use(express.static('assets'))

For more details check official docs > https://expressjs.com/en/starter/static-files.html.

answered May 31, 2021 at 21:08
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this and didn't work :( I have it in a assets/js folder and I added: app.use(express.static("/assets/js"));
you don't need to put "/assets/js" just put "assets", like app.use(express.static('assets'))

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.