Issue
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
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
Answered By - Jared Smith
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.