3

I have an Express middleware project written in TypeScript. I'd like to consume it in both JS & TS based Node projects.

I'm having trouble configuring my projects to ensure that

  • the upstream project is outputting modules that can be consumed by Node
  • my module can be consumed in JS projects in the format myGreatFunction = require('myGreatFunction') // typeof = function
  • my module can be consumed in the format import myGreatFunction from 'myGreatFunction' // typeof = function
  • my module is not being either output as an object with a .default when that is not expected, or, vice-versa, not being done so when that is indeed expected.

It feels as though I can only achieve some of these aims but not others.

What is the correct incantation of TSConfig properties (upstream & downstream) to ensure this is so?


In the end I settled on a compromise - see below.

3 Answers 3

1

Library

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "noEmitHelpers": true
  },
}

module.ts:

export class MyClass {
  static Version: string = "1.0";
}

When we compile this module we'll get:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MyClass = /** @class */ (function () {
    function MyClass() {
    }
    MyClass.Version = "1.0";
    return MyClass;
}());
exports.MyClass = MyClass;

TS Client

client.ts:

import {MyClass} from "./../src/module";

console.log(MyClass.Version);

compile and run node client.js - see "1.0"

JS Client

Just grad the same code from compiled ts client :

var module_1 = require("./../src/module");
console.log(module_1.MyClass.Version);

same output obviously

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Using a typescript file in typescript.

    Assuming B.ts is the typescript file that you want to use in A.ts, then:
    
        import { B's module that are exported } from 'path/to/B';  // notice there is no extension mentioned here.
    
    Also B must have a export module in it.
    
  2. Using a ts file in a js file.

    B.ts = file which you want to use in your main file.
    A.js = your main file.
    In your A.js:
    
        var external = require('path/to/B'); // notice there is no extension mentioned here.
    

Comments

0

In the end I settled on a compromise:

Library TSConfig:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true
  }
}

Library:

export = function myGroovyFunction() {...}

Downstream project, TypeScript

import * as myGroovyFunction from 'mygroovyfunction';

Downstream project, JavaScript

const myGroovyFunction = require('mygroovyfunction');

The TypeScript import isn't quite as concise as I'd like, but I can deal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.