SO is full of answers to this and they are all very consistent and they are all not working for me, so I'm missing something blindingly obvious.
Even when I tried to give up and use the hack of sticking <script>var exports = {};</script> before the call to require.js, I get "Uncaught Error: Module name "src/functions" has not been loaded yet for context: _. Use require([])"
Here's my minimal non-working example that seems most consistent w/ the other answers. I've also tried sticking "type": "module" everywhere I could think of.
What am I missing?
lib/
- require.js
node-modules/
functions/
- functions.ts
package-lock.json
package.json
test.html
test.ts
tsconfig.json
src/functions.ts:
export function consoleLog(msg: string): void {
console.log(msg);
};
package.json:
{
"devDependencies": {
"typescript": "^4.7.4"
}
}
test.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
</head>
<body>
<script src="lib/require.js", data-main="test"></script>
</body>
</html>
test.ts:
import { consoleLog } from "./src/functions";
function main(): void {
consoleLog("foo");
}
main();
tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "amd",
"moduleResolution": "node",
"esModuleInterop": true
},
"resolveJsonModule": true
}