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)
zedge
  • 81
  • 5
  • 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.](https://stackoverflow.com/questions/44590393/es6-modules-undefined-onclick-function-after-import) – kdau May 31 '21 at 19:06

1 Answers1

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.

  • I tried this and didn't work :( I have it in a assets/js folder and I added: `app.use(express.static("/assets/js"));` – zedge May 31 '21 at 23:32
  • you don't need to put "/assets/js" just put "assets", like app.use(express.static('assets')) – Javier Gomez Jun 01 '21 at 03:01