'Cannot find module from "undefined"
I am trying to import an external module inside my Sublime Text 3. The external module Ship.ts is a s follows:
export interface Ship {
name: string;
port: string;
displacement: number;
}
The output.ts file contains import Ship = require("./Ship"); and this file gets saved and built error-free. The tsconfig.json is as follows:
{
"compileOptions": {
"module": "commonjs"
},
"files": [
"./Ship.ts"
]
}
However, when viewed inside and index php page:
<html>
<head>
<script src="node_modules/commonjs-require/commonjs-require.js"></script>
<script src="output.js"></script>
</head>
<body>
Hello TypeScript
</body>
</html>
... there is a console error saying commonjs-require.js:15 Uncaught Error: Cannot find module "./Ship" from "undefined". The .ts and .js files along with the tsconfig.json file are all in the same directory. Any help appreciated.
UPDATE
Even if I change the contents of Ship.ts to:
export function greet(): void {
console.log("Hello Ship");
}
... which transpiles to:
"use strict";
function greet() {
console.log("Hello Ship");
}
exports.greet = greet;
... the error message commonjs-require.js:15 Uncaught Error: Cannot find module "./Ship" from "undefined" is unfortunately still on after trying to access it through either import Ship = require("./Ship") or import {Ship} from "./Ship".
UPDATE
Also tried to include ///<reference path="Ship.ts" /> or ///<reference path="./Ship.ts" /> at the top of output.ts but to no avail.
Solution 1:[1]
You are exporting an interface which doesn't generate any code, and you are trying to require it on runtime.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stijn van der Laan |
