Since I do not want to have a bunch of output JavaScript files as a result of compiling TypeScript, I am considering a Webpack for writing TypeScript NodeJS libraries. The result of implementing such a technique in most cases it will be possible to get a single index.js at the output. I repeat once again: in this question we are not talking about browser-based web applications, but about nodejs applications.

If you just build a Webpack with the node_modules / some_dependency / index.ts below , the Dependency class will not be visible in index.babel.js , because by default Webpack does not create a variable that is accessible from the outside.

index.babel.js

 require('@babel/register'); // node_modules/some_dependency/index.js (откомпилированный TypeScript) import Dependency form 'dependency'; // Error! new Dependency(); 

node_modules / some_dependency / index.ts

 export default class Dependency() { // --- } 

Unlike a web application, we have NodeJS modules, and in addition the source code in TypeScript. How to ensure the visibility of the class Dependency inside index.babel.js ?

    1 answer 1

    Minimal webpack.confing.js configuration:

     module.exports = { entry: './TypeScriptSource/index.ts', output: { filename: 'index.js', path: __dirname, libraryTarget: 'umd' }, target: 'node', module: { rules: [ { test: /\.ts?$/, use: 'ts-loader', exclude: /node_modules/ } ] }, resolve: { extensions: ['.ts', '.js'] } }; 

    Now, if the Dependency class has an export default in TypeScriptSource/index.ts , the code below in index.babel.js will work without errors:

     require('@babel/register'); import Dependency from 'some-depencency'; new Dependency(); 

    Required dependencies for index.babel.js :

     "@babel/core": "7.1.2", "@babel/node": "7.0.0", "@babel/preset-env": "7.1.0", "@babel/register": "7.0.0", 
    • Let me ask, why do you use babel in the script script node? - muturgan