question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot Load Custom Definition File in Repository

See original GitHub issue

TypeScript Version: 2.8.1

Search Terms: Definition File, Ambient type Detail I am importing a JavaScript library that doesn’t have any definition file. I want to create a custom definition file that I place in my repository. I want TypeScript to read this definition file.

I have tried many combination of tsconfig.json (typeRoot, include, path, baseUrl, etc.) Without being successful. I also moved the definition folder under src, and renamed the definition file to index.d.ts (inside a folder with the name of the library) without any success.

Code Small complete repro here: https://github.com/MrDesjardins/importdefinitionfiles

// main.ts:
import MessageFormat from "messageformat"; // Doesn't have type
// messageformat.d.ts:
declare module messageformat {
    export type Msg = (params: {}) => string;
    export interface MessageFormat {
        new(message: string): any;
        compile: (messageSource: string) => Msg;
    }
}

Expected behavior: TypeScript to find the definition file and to use it without having a compilation error.

Actual behavior: Error message

Could not find a declaration file for module 'messageformat'. '/Users/pdesjardins/code/perso/importdefinitionfiles/node_modules/messageformat/lib/messageformat.js' implicitly has an 'any' type.
  Try `npm install @types/messageformat` if it exists or add a new declaration (.d.ts) file containing `declare module 'messageformat';`

1 import MessageFormat from "messageformat";

Playground Link: https://github.com/MrDesjardins/importdefinitionfiles

Related Issues:

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
RyanCavanaughcommented, Apr 5, 2018

You need quotes around the name:

declare module 'messageformat' {

The error message tells you this:

> add a new declaration (.d.ts) file containing `declare module 'messageformat';`

In the future please use StackOverflow for questions.

1reaction
RyanCavanaughcommented, Apr 6, 2018

In fact we can check its declaration to see if it’s written as declare module foo.

Cannot find module "foo"
  An object in the global scope named 'foo' exists and can be accessed without importing it.

or (if we see declare module)

Cannot find module "foo"
  A declaration for the *global* 'foo' object exists, but was written as "declare module foo". Did you mean to write 'declare module "foo"' (with quotes)?
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixing TS2688: Cannot find type definition file in node_modules
I had "typeRoots" in package.json set to different other folders (for good but unrelated reasons) and it was forgotten to mention the otherwise“ ......
Read more >
Add a file to a repository - GitLab Documentation
Adding files to a repository is a small, but key task. Bringing files in to a repository, such as code, images, or documents,...
Read more >
Documentation - Module Resolution - TypeScript
A relative import is resolved relative to the importing file and cannot resolve to an ... the importing file, trying to locate a...
Read more >
Troubleshooting - Unity - Manual
Package Manager missing or window doesn't open · As of 2019.3, your manifest.json file should not contain any references to the com. ·...
Read more >
Declaring repositories - Gradle User Manual
For adding a custom Maven repository you can do: Example 5. ... To define an Ivy repository which fetches Ivy files and artifacts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found