'What is the 'default' part for an imported Node.js module, generated from Typescript?

In a typescript file I have a import of the filesystem and path Node modules. I use them in a pretty standard way, like:

const workDir = path.join(outputDir, "process-specs");.

When I transpile that using tsc it generates this line instead:

var workDir = path_1.default.join(outputDir, "process-specs");

The problem with that is the additional default member of the path module variable. I don't see it in the Node.js path documentation and wonder why tsc adds that and what this is about.



Solution 1:[1]

It's aping the default export of es2015 modules: when you do import foo from 'foo'; you are importing the default export of the foo module.

// foo.ts
export default foo;

// otherfile.ts
import foo from 'foo';

vs a named export

// foo.ts
export foo;

// otherfile.ts
import { foo } from 'foo';

If this is only running in node.js and not the browser you can just use require like normal, e.g. const fs = require('fs');. You'll need to install node typings so the compiler understands it:

npm install --save-dev @types/node

Solution 2:[2]

allowSyntheticDefaultImports in tsconfig.json is why default was generated for tsc. and importHelpers, esModuleInterop assume more advanced way to use.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2