1

I've installed md5 (also tried blueimp-md5) package with corresponding typings like this:

nmp install --save md5 @types/md5

nmp install --save blueimp-md5 @types/blueimp-md5

When I try to import it like this:

import { md5 } from '../../../node_modules/md5'

I get an error: Module <path> was resolved to <path>/md5.js, but '--allowJs' is not set.

Error in VS Code

This makes me think that installed @types/md5 typings are simply not discovered. In tsconfig.json I have:

"typeRoots": [
  "../node_modules/@types"
]

So I think it should be detecting typings from node_modules/@types folder automatically, but it apparently does not. Exactly same thing with blueimp-md5 package. The md5 folder exists in node_modules/@types folder, so it has everything in place but still doesn't work.

Visual Studio Code, TypeScript 2, Angular 2 project.

What am I doing wrong?

Edit: this is content of @types/md5/index.d.ts file:

/// <reference types="node" />

declare function main(message: string | Buffer): string;
export = main;
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Titan
  • 2,875
  • 5
  • 23
  • 34

1 Answers1

2

You don't need to specify the path inside the node_modules, it should be:

import * as md5 from "md5";

The compiler will look for the actual module in the node_modules, and will look for the definition files in node_modules/@types.

There's a long doc page about it: Module Resolution


Edit

That's because of how the md5 module is exporting, as it does this:

declare function main(message: string | Buffer): string;
export = main;

This case is covered in the docs:

The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

When importing a module using export =, TypeScript-specific import let = require("module") must be used to import the module.

In your case it should be:

import md5 = require("md5");

2nd edit

If you're targetting es6 then you need to do:

const md5 = require("md5");

(or let or var of course).

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • 1
    When I specify just "md5" it gives error: `Module '"/node_modules/@types/md5/index"' resolves to a non-module entity and cannot be imported using this construct.` – Titan Dec 20 '16 at 21:30
  • @Titan What is ``? Did you configure your TS to to use node as the module resolution? – Ruan Mendes Dec 20 '16 at 21:32
  • Check my revised answer – Nitzan Tomer Dec 20 '16 at 21:36
  • When I use `import md5 = require("md5");` syntax, I get this error: `Import assignment cannot be used when targeting ECMAScript 2015 modules.` – Titan Dec 20 '16 at 21:53
  • Well I ended up using a different module specifically coded for TS, and importing it like this: `import { Md5 } from 'ts-md5/dist/md5'`. But I'm accepting your answer for any future readers. – Titan Jan 16 '17 at 10:39