Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is @types/node package in NodeJs?

Tags:

node.js

I'm using Node v10 and I found that I have dependency issue when using a library.

In the ldapjs library, lib\url.js uses const url = require('url') and then parsedURL = new url.URL(urlStr) to parse the string to URL.

I get this error:

Unhandled Rejection (TypeError): url.URL is not a constructor

with this line of code: parsedURL = new url.URL(ldapurl) in my React project.

However, under my environment, 'url' actually refers to node_modules\@types\node\url.d.ts which doesn’t contain any .URL(str) method, thus the error.

What is @types/node? Is it related TypeScript? I don't use TypeScript in my project. Can I remove it?

If I run Node in the terminal, it works fine:

> const url = require('url')
undefined
> let purl = new url.URL("https://google.com")
undefined
> console.log(purl)
URL {
  href: 'https://google.com/',
  origin: 'https://google.com',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'google.com',
  hostname: 'google.com',
  port: '',
  pathname: '/',
  search: '',
  searchParams: URLSearchParams {},
  hash: '' }

What is the problem?

like image 296
Gordon Avatar asked Dec 02 '20 07:12

Gordon


People also ask

What is Types node in package json?

This package contains type definitions for Node. js (http://nodejs.org/). This package is used to load in all type definitions when using typescript in node. When you add other packages, you also have to add their typings if they do not include them by default.

What are Nodejs packages?

What is a Package? A package in Node. js contains all the files you need for a module. Modules are JavaScript libraries you can include in your project.

How many node packages are there?

How many node packages are there? There are 350,000 node packages. NPM registry contains the most populated package registry. This is a fact that it is considered to be the largest package registry in the world.


1 Answers

As taken directly from the @types/node npm package:

This package contains type definitions for Node.js (http://nodejs.org/).

This package is used to load in all type definitions when using typescript in node. When you add other packages, you also have to add their typings if they do not include them by default.

As an example, imagine you want to use lodash in your typescript application, you will need to install the type definitions (@types/lodash) too.

You don't need to worry about all this when you are not using typescript.

like image 103
Titulum Avatar answered Oct 25 '22 02:10

Titulum