how to use #types/node in node application - node.js

I am working in VSCode on Ubuntu 16.04. I've created node project using below commads:
npm init
tsc --init
I've created a new file called index.ts. I'm trying to use fs and readling to read file contents. but when I am writing below lines of code at the top of index.d.ts:
import fs = require('fs');
import readline = require('readline');
I'm getting below error:
can not find module 'fs' and can not find module 'readline'
even process is not found.
I've installed typings of node from here using below command:
sudo npm install #types/node -global --save
Can anyone please help me how to resolve this error?

Since TypeScript 2.x, all typings are installed using npm like this: npm install #types/node.

For TypeScript 1.8, it might be better to typings to install the node types. For detail see the quickstart at: https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html.

For what i know you have two options here:
(Recommended) Install devDepencencie npm install #types/node --save-dev, which will add the type module for http.
Create a index.d.ts file declaring a definition for http module, like:
declare module 'http. This method will not enable auto-complete for http methods

Related

Loading module installed directly from github repo with npm

I installed a public github repo using the syntax provided in the repository's readme:
npm install https://github.com/user_name/proj_name.git
However, I'm not sure how to load this module into my js code. I've tried:
const my_lib = 'proj_name';
Unfortunately, this is not working for me. How can I load a module that was installed directly from a github repository?
Currently you're just defining a constant as a string with value 'proj_name'.
To load a module from node_modules, you have to do the following:
npm install <package_name> --save, where --save write the package and version in your dependencies of package.json. You can also use --savedev to write the package in your devDependencies (both optional).
Use const packageName = require('packageName'); in your e.g. app.js to use the package in your code.
See here for more details about npm install in general, specifying-dependencies and here for ecma script require and import.

How to import node-js dependency in webpack project if package already includes types definitions?

chalk library already includes types definitions, and there is no need to use deprecated #types/chalk package. However, I got error TS2307: Cannot find module 'chalk'.
The chalk library project structure is:
What I did wrong? I tried colors.js library instead - same error.
Node: my project is build by Webpack for Node.js. Off course, webpack has been configured for node (includes target: 'node' option and nodeExternals plugin usage).
Link to project with problem reproduction:
https://drive.google.com/file/d/1Aro46dXM9rTw6IsaTv5TEzzXEp_sCJTQ/view?usp=sharing
Node.js installation required, and as always, npm i command before run project building.
You need to fix it manual by following command:
npm install chalk
npm install each-async
npm install indent-string
in case it doesn't work you need to clear cash and check the config in webpack.
In my case, "moduleResolution": "node" was missed in tsconfig.json.

Why I dont need type definitions for node when working with Typescript?

When I use nodejs with Typescript it allows me to use modules from nodejs (like fs) although I didn't install #types/node.
Why? Are they somehow baked in?
It does require #types/node:
echo "import * fs fs from 'fs';" > test.ts
tsc test.ts
Results in
test.ts:1:21 - error TS2307: Cannot find module 'fs'.
1 import * as fs from 'fs';
Then doing a
npm install #types/node
tsc test.ts
runs fine.
Do you possibly have #types/node installed globally?
When you are using a dependency which already has a dependency on #types/node, then you don't need to define the #types/node dependency yourself because it is hoisted.
You can find out if it is hoisted by running yarn why #types/node, which will show you information about why a package is installed.

I get an error using NodeJs modules

I've created my first NodeJS module using lodash. When I install my module using npm i mymodule --save and run my index.js script using require('lodash') I get an error with a sub module...
Error : Error: Cannot find module 'lodash'
lodash is well installed but not in the root directory, in the modules directory...
In the source tree of your module you need to run:
npm install lodash --save
And then publish a new version of your module to npm.
The package.json of your module needs to have lodash in its dependencies. If it doesn't then it will not get installed together with your module.

Typescript Definitions from Node Modules

I am trying to configure typescript with my tsconfig.json so it does find definitions inside node_modules.
Ive read that it should work if i set the module to commonjs despite excluding node_modules.
I tried to remove the exclude. Tried to include manually but nothing works.
How to configure typescript to find definition files installed with the source npm package?
Typescript#2.0 (Recommended)
You may use npm install. Example:
npm install --save #types/<your package name>
Typescript#1.x
Use tsd package: https://github.com/DefinitelyTyped/tsd
I also have node.js + typescript#2.0 + mongodb + passportjs example right there: https://github.com/thanhtruong0315/typescript-express-passportjs.

Resources