Import from installed @types?

I installed the md5 package (also tried blueimp-md5 ) with the corresponding types:

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 the @types/md5 installed vise is simply not detected. In tsconfig.json, I have:

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

So, I think this should automatically detect node_modules/@types from the node_modules/@types folder, but apparently this is not the case. Exactly the same thing with the blueimp-md5 package. The md5 folder exists in the node_modules/@types folder, so it has everything in its place, but still does not work.

Visual Studio Code, TypeScript 2, Angular 2.

What am I doing wrong?

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

 /// <reference types="node" /> declare function main(message: string | Buffer): string; export = main; 
+1
angular typescript visual-studio-code
source share
1 answer

You do not need to specify the path inside node_modules , it should be:

 import * as md5 from "md5"; 

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

There is a long document page: Module Resolution


Edit

This is because of how the md5 module is exported, since it does this:

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

This case is in the docs :

The syntax export = indicates a single object that is exported from a module. It can be a class, interface, namespace, function, or ENUM.

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

In your case, it should be:

 import md5 = require("md5"); 

2nd edit

If you are targeting es6 , then you need to do:

 const md5 = require("md5"); 

(or let or var , of course).

+2
source share

All Articles