My NodeJS project is written in Typescript 2.5
The following import line for the module:
import log4js= require('log4js);
compiles and i use the logger successfully. Even IntelliSense prompts the object method and properties well. Yet, i got following the annoying error from IntelliSense:
TS2307 (TS) Cannot find module 'log4js'.
If i try to add the respective types to my project but i got the message:
npm install #types/log4js --only=dev
npm WARN deprecated #types/log4js#2.3.5: This is a stub types definition for log4js (https://github.com/nomiddlename/log4js-node). log4js provides its own type definitions, so you don't need #types/log4js installed!
Related
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:
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.
I am currently trying to develop an app using Ionic 2 and Angular 2 with Typescript version. I decided to use the library amqp-ts to include messaging in my app. I installed the library via npm like:
npm install amqp-ts
Everything went fine and now I've got something like this:
/ app root directory
+ node_modules
- amqp-ts
- lib
- amqp-ts.d.ts
- node_modules
- amqplib
- bluebird
- winston
The problems begin now: I import the library into my component as it is done in the example of the documentation...
import * as Amqp from "amqp-ts";
... and when I try to deploy the app I get the next error messages:
TypeScript error: C:/APPs/Test/Ionic2Angular2App/node_modules/amqp-ts/lib/amqp-ts.d.ts(2,26): Error TS2307: Cannot find module 'bluebird'.
TypeScript error: C:/APPs/Test/Ionic2Angular2App/node_modules/amqp-ts/lib/amqp-ts.d.ts(50,12): Error TS2304: Cannot find name 'Buffer'.
1. The line related to the first error message
// exported Typescript type definition for AmqpSimple
import * as Promise from "bluebird";
[...]
2. The line related to the second error message (same file: amqp-ts.d.ts)
export class Message {
content: Buffer;
[...]
}
I hope you can help me, please.
Additionally to regular package install you need to install TypeScript typings. Typings are like header files, they contain all methods/classes/interfaces definitions.
To install typings you need a typings tool. The best way is to install it globally so you can use it in every project
npm install typings --global
Then installing new typings inside your project is pretty simple, first search for library:
typings search bluebird
Install it :
typings install --save bluebird
More info : https://github.com/typings/typings
I too faced same issue, but for me above answer is not working.
While simply running :
npm i bluebird
resolving the issue
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 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.