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)
1 Answer 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.
Sign up to request clarification or add additional context in comments.
2 Comments
zedge
I tried this and didn't work :( I have it in a assets/js folder and I added:
app.use(express.static("/assets/js"));Javier Gomez
you don't need to put "/assets/js" just put "assets", like app.use(express.static('assets'))
lang-js
window) namespace, so you can't call them from an HTML attribute without more setup. This question and its two answers explain further.