Home > OS >  Ionic 2 cannot find module 'dgram'
Ionic 2 cannot find module 'dgram'

Time:08-12

I have installed a template Ionic 2 application and want to add the NPM package bonjour

After installing and including the package in my component like this:

var Bonjour = require('bonjour');
var bonjour = new Bonjour();

The application won't run stating 'cannot find module dgram'

The application has both the bonjour package and bonjour types installed.

The problem

The application can't find the module dgram which is located in the @types/node file. The project is running TS 2.4.2 and should not need any references to the @types, this should be picked up automatically.

What have I tried

I tried including the @types folder anyway in multiple ways, by setting typeroots or types in the ts.config.json file. This didn't change anything.

I tried specifying types :

"types": ["node", "bonjour"]

I tried reinstalling all node modules and clearing the cache

I tried including a reference path in my component above the require statement:

/// <reference path="node_modules/@types/node/index.d.ts" />

var Bonjour = require('bonjour');
var bonjour = new Bonjour();

This all did not help. Any ideas on how to make my application load this module properly?

CodePudding user response:

The package Bonjour has a DatagramPlugin which require dgram to function properly. In Ionic 2 this package is not available. The solution is to use the Native Zeroconf package as an alternative.

CodePudding user response:

dgram library is included with node.js since v0.1.99 as seen here.

You will always have dgram defined as long as you use a node version post v0.1.99. Your problem is only with Typescript types.


Make sure you are installing node types with npm i --save-dev @types/node and that you are including the es6 lib in your tsconfig.json file.

If the previous step does not work add this on the top: import * as dgram from "dgram";

If nothing works you can copy the module definition from here export it yourself.


Extra tip: If you do not trust your tsconfig.json for some reason pass the lib and types argument directly in the tsc command such as: tsc --lib es6 --types node -p .

  • Related