Giter Club home page Giter Club logo

typescript-node-es-modules-example's Introduction

Node ES Modules with TypeScript

Latest Typescript and Node - as bare-bone as possible example app

I wanted to start a simple Node project where I can install the latest things I want and using as little compilation from Typescript as possible.

I remembered Node announced Node 13 support ESModules without experimental flags, checked out the official documentation but couldn't find a simple bare bone example, so maybe that would help someone.

I've committed the dist folder as well to see how the output from Typescript.

Would love suggestions on how to make it better or to point to a better example!

What I've done (so you can tell me what I should have done...)

Node (package.json)

  • type: module: Tell Node that .js files are ES Modules. (When file extensions are .mjs then type=module is not required in package.json)
  • ----experimental-specifier-resolution=node: Tell Node to enable the automatic extension resolution and importing from directories without specify the .js extension.

    Note: By default, Node wants file extensions in import see: TypeScript doesn't include file extension in it's output

TypeScript (tsconfig.json)

  • "module": "esnext": Make Typescript output ES Modules
  • "target": "es2020": Highest target (from 3.8.0)

Run app

  1. Install latest Node (Notice that I've placed the engines field on package.json so try to make sure you have the exact version or simply delete it from package.json
  2. Install dependencies - yarn
  3. Compile with tsc -w and run Node with nodemon - yarn dev
  4. You can also use yarn compile and yarn start separately

Want to import from a commonjs package?

Here is a branch of the example with including a commonjs pacakge (pg):

https://github.com/Urigo/typescript-node-es-modules-example/commit/98304173e964713955be3a92b4e355a857376dca

The renovate.json file has nothing to do with the project itself. It's a file to activate RenovateBot to automatically PR this repository when a new version of any dependency of that project has been published

typescript-node-es-modules-example's People

Contributors

serkansipahi avatar urigo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.