Giter Site home page Giter Site logo

ts-paths-node-native's Introduction

ts-paths-node-native

This repository shows how to use TypeScript paths with Node.js without any additional bundling/transformation step.

Usage

$ git clone [email protected]:fox1t/ts-paths-node-native.git
$ npm i
$ npm run build
$ npm start

Note

If you use a Node.js version that doesn't support the subpath-patterns feature, you will get this error:

internal/modules/cjs/loader.js:969
  throw err;
  ^

Error: Cannot find module '#subpath/index'

Background

Before version v12.20.0 and v14.13.0, in order to use TS path-mapping in Node.js, a bundling/transformation step was mandatory. In fact, TypeScript itself doesn't "convert" the mapped paths to the real paths. That's it if you write:

import { foo } from "#subpath/foo";

it will be compiled to

const foo_1 = require("#subpath/foo");

Possible ways of making it work were to use babel to transform the code during the build or add a dependency at runtime using the -r flag. Neither of two was an ideal one:

  • Using another tool in conjunction with tsc adds complexity to the configuration: in this case, tsc was used just to check types and the compilation was usually handled by babel
  • Adding a runtime dependency has two significant drawbacks:
    • slower startup time of the process;
    • there are still no "mature" projects that work in every scenario.

Solution

Version 12.20.0 and 14.13.0 Node.js supports native path remapping during require/import thanks to two features:

Adding these lines to the package.json file

"imports": {
    "#subpath/*": {
      "require": "./dist/subpath/*.js"
    }
  },

makes Node.js understand what to do when it finds const foo_1 = require("#subpath/foo"); in the code. On the tsconfig.json side, things remain the same as before.

{
  "compilerOptions": {
    "outDir": "dist",
    "baseUrl": "./src",
    "paths": {
      "#subpath/*": ["subpath/*"]
    }
  }
}

Limitations

Always use # to specify an import subpath

Entries in the imports field must always start with # to ensure they are disambiguated from package specifiers. So no more @.

Use index file when importing folders

Since Node.js' subpath imports just add .js extension to the path you provide, you need to use the index file when importing a folder. That's it,

import * as config from "#subpath/module/index";

instead of

import * as config from "#subpath/module";

In fact, the latest one will be resloved as

`const module = require("./dist/module.js")`

instead of

`const module = require("./dist/module/index.js")`

and Node.js will not find the file to import at runtime.

No JSON files import

Today, importing .json files in TypeScript using the flag resolveJsonModule isn't supported since Node.js matches files without extension. So writing

import * as config from "#subpath/config.json";

Will resolve to this path.

./src/subpath/config.json.js

Use the full path instead of the alias.

ts-paths-node-native's People

Contributors

fox1t avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.