What is the difference between require(x) and import x in TypeScript and Node.js?
Hello! I started learning TypeScript and Node.js, and I got confused about module imports. I see that some examples use `require('module')`, while others use `import module from 'module'`. For example:\n\n```typescript\n// Option 1: require\nconst fs = require('fs');\n\n// Option 2: import\nimport fs from 'fs';\n```\n\nBoth options seem to do the same thing — they include the `fs` module. But I noticed that when I write `require`, TypeScript often complains about types, while `import` works fine. I also heard that `require` is the old way, and `import` is the new way. But what exactly is the difference? Can they be mixed in the same project? And which method is better to use in 2024?\n\nI'm working on a Node.js project with TypeScript, and I want to understand how to properly import modules so that
No answers yet.
Log in to leave an answer
Blogs