With the latest features and tools in Node.js, setting up a modern TypeScript project has never been easier—or more exciting. This guide will show you how to leverage the newest Node.js capabilities to create a lightweight and efficient development workflow.
Why Choose This Setup?
This setup emphasizes simplicity, native features, and minimal dependencies by leveraging:
- Native ESM (ECMAScript Modules) for cleaner, modern syntax.
- Experimental TypeScript Stripping (--experimental-strip-types) for running TypeScript files natively.
- Built-in File Watching without third-party tools like nodemon
- Native Environment Variable Support with .env files
Explore the repo for this setup: esm-pure-experimental-strip-types.
Node.js ESM
ECMAScript Modules (ESM) bring modern syntax with import and export.
Benefits of ESM:
- Performance: Static analysis of imports/exports improves optimization compared to CommonJS (CJS).
- Simplified Module Resolution: Requires explicit file extensions, speeding up module resolution. For example, instead of:
const module = require('./module');
You now write:
import module from './module.js';
Experimental TypeScript stripping (--experimental-strip-types)
The --experimental-strip-types flag lets you run .ts files directly in Node.js, eliminating the need for transpilers like Babel or SWC. Here's how to use it:
node --experimental-strip-types index.ts
Requirements:
- Use ESM syntax.
- Include explicit file extensions (e.g., .ts) in imports.
Here’s a sample tsconfig.json to get you started:
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"isolatedModules": true,
"resolveJsonModule": true,
"outDir": "./dist",
"rootDir": "./src",
"noEmit": true,
"types": ["node"],
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"incremental": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist"]
}
Key Configurations:
- allowImportingTsExtensions: Ensures .ts extensions are required in imports.
- verbatimModuleSyntax: This avoids altering your import/export syntax. By using --experimental-strip-types, you can bypass tools like Babel, SWC, Webpack, or TSX, streamlining your toolchain significantly.
Built-in File Watching (--watch)
Forget nodemon! Node.js now supports file watching natively. Use the --watch flag for live reloads:
node --watch index.ts
Benefits:
- Faster and more efficient than nodemon.
- Native support means fewer dependencies.
Native Environment Variable Support (--env-file)
You no longer need packages like dotenv or dotenv-safe to handle .env files. Simply use the --env-file flag:
node --env-file=.env index.ts
Advantages:
- Seamless integration with .env files.
- Fully native functionality, reducing dependency bloat.
Closing Thoughts
This modern Node.js + TypeScript setup eliminates unnecessary complexity by leveraging the latest features. With native ESM, experimental TypeScript stripping, built-in file watching, and .env support, you can:
- Reduce dependencies.
- Improve performance.
- Simplify your workflow.
By embracing these tools, you’ll boost productivity and focus on building, not configuring.
What are your favorite Node.js + TypeScript features? Let me know in the comments!
Woovi is a fintech platform revolutionizing how businesses and developers handle payments in Brazil. Built with a developer-first mindset, Woovi simplifies integration with instant payment methods like Pix, enabling companies to receive payments seamlessly and automate financial workflows.
If you want to work with us, we are hiring!
Hey, I'm looking for a role.
Here is my portfolio bhataasim-portfolio.vercel.app/