How do I use http in a typescript server

import http from 'http'

it doesnt find the http module or any node modules

As far as I know, using es modules in node requires .mjs extension.
Take a look at the node docs here: https://nodejs.org/api/esm.html

trying to use with typescript which uses .ts

Try this:

import http = require('http');

OR

import * as http from 'http'

require doesnt work in TS and it cant find 'http' module with import

I haven’t used Typescript with node myself, so I can’t test it.

I took the examples from here:
https://www.typescriptlang.org/docs/handbook/modules.html

I see that the Microsoft Typescript starter repo is using normal imports? There might be some additional config that you’re missing perhaps. Check it out:

Seems like you may need to add "module": "commonjs", into your tsconfig if you’re missing it

2 Likes

import * as http from "http"; + the config is what’s necessary 99% of the time I’ve had issues

1 Like

doesnt work

npm i @types/node

this fixed it