Default imports after publishing a NPM package

Viewed 132

Suppose I am writing some Typescript and in my index.ts I have something like this:

import { Something } from './something';
export default Something;

Then this gets compiled to:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var something_1 = require("./something");
exports.default = something_1.Something;

Now suppose I publish this to NPM and everything goes well. I install this package and then I initiate a new project to test this thing, so I run npm init -y and create a simple index.js like this:

import Something from "my-npm-package"
console.log(Something);

This prints out:

{ default: [Function: _default] }

So in order to use Something I have to actually say:

const actuallySomething = Something.default;

So how do I simply get Something.default without the .default part?

UPDATE: The package.json from my NPM package is here: https://github.com/DavidArmendariz/identity-number-validator/blob/master/package.json

I also know that if I transpile my code with Babel by simply doing this:

npx babel index.js -o out.js

And then run

node out.js

Then everything works as expected. But is there any way to do this without Babel? For example, if you install lodash you will be able to import the functions like this:

import _ from "lodash"
const example = [1,2,3]
_.map(example, function(n) { n*n })

And that's because lodash "exports" the functions right away (I really don't know how this is done, or if Typescript alone is not sufficient to do this)

0 Answers
Related