2

How do i get declare module to work in Node, I get the typescript to compile without errors and the Intellisense in VS.Code works. But i get "Cannot find module 'messages'" in runtime.

Clarification : I´m trying to get both the api.ts and mq.ts classes under the same "namespace" messages.

I have the following node project setup.

  • /messages/api.ts
  • /messages/mq.ts
  • /main.ts

api.ts

declare module "messages" {
export class Put {

    }
}

mq.ts

declare module "messages"{
export class GetWork {

    }
}

main.ts

import * as messages from "messages";
let x = new messages.GetWork();

tsconfig.json

{
   "compilerOptions": {
      "target": "es6",
      "module": "commonjs"
   },
   "exclude": [  ]
}

jsconfig.json

{
   "compilerOptions": {
       "target": "ES6"
   }
}

2 Answers 2

3

In node you don't need to use declare module, every file is just a module, declare module is for d.ts and other usage.

In your case just add an index.ts under /messages directory like this and remove declare module.

import * as M1 from "./M1";
import * as M2 from "./M2";

export {M1, M2};
Sign up to request clarification or add additional context in comments.

4 Comments

The thing i want to achieve is to get both files classes under the same namespace, messages.Put and messages.GetWork
@JohanLindberg just write one messages.ts and export Put and GetWork, or use index to reexport the submodules, declare module and namespace is needless here.
Then i have to export every class i make, I'm trying to get the same behavior as a normal C# project, where i can have multiple files targeting the same namespace without needing to double code somewhere.
I went with your first suggestion so i got the following stucture. messages.mq.getwork and messages.api.put which is better then my first intention but it doesn't exactly solve my original question. The export part does but it do require some extra coding every time i make a new class.
1

A few things here, because you're trying to import messages without a relative path, with just the names, what TypeScript tries to do is to find a module in a node_modules folder. That's why it can't find any.

So, if you want to import one of your own modules you should use a relative path.

Now, every file is a module. So if you have a file called mq.ts you should import it as follows:

import { Put } from './mq';

The syntax:

declare module "messages" {
  // ....
}

is used only when creating Typings for existing node_modules and usually one would create a .d.ts file to do so.

Here's the documentation on module resolution for TypeScript, it is a good one.

4 Comments

I'm aware of this, the reason I wanted to use declare module was that i wanted both the files classes to be under the same "namespace". Both messages.Put and messages.GetWork.
@JohanLindberg that's something that is not recommend by the TypeScript documentation, check the Needless Namespacing section on typescriptlang.org/docs/handbook/namespaces-and-modules.html
The part in Needless Namespacing isn't what I'm doing or trying to achieve. I want the same functionality as in a C# project where multiple files can target the same namespace. Like their shapes.ts, i want another file moreshapes.ts that also get included in my shapes namespace. psuedo "import * as shapes from "./shapes && ./moreshapes";
I don't think you can do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.