2

I am making a game in which I have a js class that I am exporting as follows:

var Game = cc.Class({
    name: "WS",
});
module.exports = Game;

The above is in a file ws.js.

In the same directory as ws.js, I have a TypeScript file called transitions.ts where I try to import the Game module as follows:

import Game = require ("./Game");

I am getting an error: cannot find module './Game'

7
  • Do you use webpack? Commented Mar 22, 2019 at 11:33
  • This isn't a JavaScript/TypeScript thing, it's to do with what you're using to handle require calls -- Webpack, RollupJS, Node.js... Commented Mar 22, 2019 at 11:34
  • 1
    Uh, use require("./ws.js") if that's what the file is called, not Game.js? Commented Mar 22, 2019 at 11:35
  • "Also, I would like to know..." Ask one question per question, not two or three. I was doing some cleanup on the question, so I removed that bit. Commented Mar 22, 2019 at 11:36
  • That import Game = require ("./Game"); looks weird, but it's valid in TypeScript. Commented Mar 22, 2019 at 11:45

2 Answers 2

1

Exporting your class as a module will allow you to import it as one in another file.

Class House { blah.. blah };
export default House;

import House from '../filename';

Sign up to request clarification or add additional context in comments.

Comments

1

Import the JS module as any

Just replace import with const:

const Game = require("./Game");

Or, write a module definition first

The other solution is to describe the JavaScript module in a .d.ts file:

// Game.d.ts
declare class Game {
  // …
}
export = Game;

Then, you can import the JS module:

import Game = require("./Game");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.