Ionic 2 cannot find module 'dgram' - node.js

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?

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.

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

Publish Typescript Interfaces with NPM

I'm working in a project that has multiple typescript projects. I'm trying to have a common package for all the interfaces, types, enums, etc.
I thought I could make it work creating a NPM package with Typescript and have an index.ts with this content:
When I'm working in the projects that depend on this package, everything seems fine, but when I want to start the development environment I'm getting this error:
I've got the suggestion of running ts-node with --skipIgnore flag, but I'm getting the same error:
Maybe I needed to compile the code and import the .js (doesn't make ANY sense, but at this point 🤷🏽‍♂️)... no luck.
Let me share both tsconfig.json:
The one from the "common" package:
The one from the project that depends on the common package:
Things suggested and tried:
Because your target is ES6, commonjs modules cannot use your package. The dependent project should change its module type to ES6 as well - #kellys
Changing project's module to ES6, brings me this error:
All right, let's add moduleResolution: "node" ... and I'm getting:
So I'm adding "type":"module" in package.json:

How to create a simple (Hello World) node.js TypeScript project with ES6 modules?

Imagine a very simple program with a main file and some functions in a separate file, using ES6 modules and intended to be run with node.js.
my-utils.ts:
import * as fs from "fs";
export function readSimpleFile() {
return fs.fileReadSync("hello.txt", "utf-8");
}
main.ts:
import {readSimpleFile} from "./my-utils"
console.log(readSimpleFile());
What is the minimum set of files I need to add to the project and commands I have to run to get it building, running and checking types?
If you are to run a typescript project with node you need to have at least node, npm and typescript installed on your plateform.
Using an IDE to setup the project
Using intelliJ IDEA or Webstorm (they are the ones I know the best), the compilation of typescript into javascript is done automatically; you only need to do some settings.
Let us assume you have a file called project.ts containing your hello world code; IDEA or Webstorm will compile your code to project.js. Then you will only need to do node project.js to run your project.
Doing everything from scratch
First you need to know where exactly your npm packages are installed globally. This command can help you identify the path: npm config get prefix. In this folder, you should have a nodes_modules subfolder that contains the typescript module. If there is no typescript module, that is because you did not install typescript globally (npm install -g typescript).
Then you have to add the path of the bin of typescript subfolder in your environment variable.
Now you can compile the project with typescipt: tsc project.ts and you can run it node project.js.
Since you are using node function like fs you will need to install node typings npm install #types/node --save-dev before compiling with tsc.
Compilation option
To enable or disable all strict type checking options, you might need to use compilation option. You have to create the file in which you will specify the compilation option: tsc --init will create a tsconfig.json in which you can specify what behaviour you would like to have during the compilation of your app. All options are listed here.

Include Typescript library in another Typescript library

I've a github typescript library which I'd like to install using
npm install --save git+ssh://git#github.com:User/mylib.git
in my target node service which is also in Typescript.
How do I get this done with type safety? I'd like to know if there are any type mismatches when I compile my service.
I'd like to know if there are any type mismatches when I compile my service.
Make sure your library has :
tsconfig.json : outDir and declaration:true
typings pointing to the outDir
Example
TypeStyle ships with TypeScript definitions : https://github.com/typestyle/typestyle
This was what we used finally:
In the typescript library being exported (#myuser/lib1), we added the following line in package.json
"types": "./src/index.ts" or "./src/index.d.ts"
In the typescript service using the above library, we imported it as:
npm install --save #myuser/lib1
This installed the library with types and compiles your service with type safety checks.

TypeScript won't resolve external module (node.js)

I would like to use moment.js in my node application, so I installed moment.js using node's package manager npm:
npm install moment#2.4.0
Just to be on the safe side, I checked moment is not installed globally and the installed version is really version 2.4.0 (version 2.4.0 in order to use the correct d.ts file ...)
require("moment").version
Alright, seems to be good. I'm also using the latest version of TypeScript (0.9.5).
So, now I added the following file to my projects root directory https://github.com/borisyankov/DefinitelyTyped/blob/master/moment/moment.d.ts and refernced the file:
/// <reference path="moment.d.ts" />
Now, it should work to import moment using TypeScripts import keyword:
import m = require("moment");
Compiling with the following command
tsc app.ts --module commonjs
produces the following errors
/home/unknown/temp/test/app.ts(3,1): error TS2071: Unable to resolve
external module '"moment"'. /home/unknown/temp/test/app.ts(3,1): error
TS2072: Module cannot be aliased to a non-module type.
Why does this error occur? How do I fix it?
The important line in the d.ts file is this one...
declare var moment: MomentStatic;
It just declares a variable for moment.
You can add the following line to resolve your problem:
export = moment;
This should make it loadable using the import statement you have.
If you do this - you won't need yhe reference comment.

How to NodeJS require inside TypeScript file?

How do I load a regular NodeJS module (from node_modules) from within a TypeScript class?
When I try to compile .ts file that contains:
var sampleModule = require('modulename');
Compiler prompts that I can't use require in this scope. (That line is at the beginning of the file).
Typescript will always complain when it is unable to find a symbol. The compiler comes together with a set of default definitions for window, document and such specified in a file called lib.d.ts. If I do a grep for require in this file I can find no definition of a function require. Hence, we have to tell the compiler ourselves that this function will exist at runtime using the declare syntax:
declare function require(name:string);
var sampleModule = require('modulename');
On my system, this compiles just fine.
The correct syntax is:
import sampleModule = require('modulename');
or
import * as sampleModule from 'modulename';
Then compile your TypeScript with --module commonjs.
If the package doesn't come with an index.d.ts file and its package.json doesn't have a "typings" property, tsc will bark that it doesn't know what 'modulename' refers to. For this purpose you need to find a .d.ts file for it on http://definitelytyped.org/, or write one yourself.
If you are writing code for Node.js you will also want the node.d.ts file from http://definitelytyped.org/.
The best solution is to get a copy of Node's type definitions. This will solve all kinds of dependency issues, not only require(). This was previously done using packages like typings, but as Mike Chamberlain mentioned, Typings are deprecated. The modern way is doing it like this:
npm install --save-dev #types/node
Not only will it fix the compiler error, it will also add the definitions of the Node API to your IDE.
Use typings to access node functions from TypeScript:
typings install env~node --global
If you don't have typings 😲 install it:
npm install typings --global
when loading typescript module I use module name with extension i.e.
".ts"
const sampleModule = require('modulename.ts');
FYI, in my case "node" command (v14.17.1) using ".ts" files directly without generating "*.js" files.

Resources