How do I use my typescript module with node, without compiling it?

Viewed 325

I have a typescript module that I am trying to reuse inside of a few other projects/modules.

package.json:

{
  "name": "shared-module",
  "version": "1.0.0",
  "private": true,
  "description": "",
  "main": "src/index.ts",
  "scripts": {
    "compile": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies":  {
    "typescript": "^4.5.2"
  },
  "dependencies": {
    "@types/node": "^16.11.11",
  }
}

Now when I link(import) this module into a Svelte-Kit project of mine, I am able to use the exports from the module without even compiling the module. The hot reloading works and upon changing the module src files the svelte app picks up the changes.

Now I want to set up something similar with an empty node project of mine, where I import this module and I don't have to recompile it every time I want my parent module to pick up the changes. However I am getting

TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "file:///E:/module-dir/src/index.ts" 

when I try to run the parent module with:

tsc && node --experimental-modules --es-module-specifier-resolution=node dist/index.js"

How do I get a similar behavior to Svelte-Kit? I.e. the typescript module is imported and everything is compiled with the parent project?

Is it even possible out of the box with node, or does it work with svelte kit due to internal packaging magic from vite?

0 Answers
Related