Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import * as stripAnsi from "strip-ansi"; #11

Closed
timkraut opened this issue May 31, 2017 · 5 comments
Closed

import * as stripAnsi from "strip-ansi"; #11

timkraut opened this issue May 31, 2017 · 5 comments

Comments

@timkraut
Copy link

I tried to import stripAnsi with import * as stripAnsi from "strip-ansi"; in a TypeScript-Node.js application but this fails with the error message Module '"/.../node_modules/@types/strip-ansi/index"' resolves to a non-module entity and cannot be imported using this construct.. The issue seems to be that a function is exported (see e.g. microsoft/TypeScript#5867 (comment)). Would it be possible to change that to something consumable by TypeScript? Or is there any other way I might get this to work?
I know that this module isn't doing much and that I can easily write such a function on my own but I thought maybe other people are going to have the same issue as well so maybe we can try to find a solution for it.

@kevva
Copy link
Contributor

kevva commented May 31, 2017

import stripAnsi from 'strip-ansi'. It only exports a function. Maybe have a look here for more info https://github.com/DefinitelyTyped/DefinitelyTyped/tree/f2b6506ba550e909224e43ecd07e15bbfbfbfce3/types/strip-ansi.

@timkraut
Copy link
Author

@kevva Thanks for your quick answer! Unfortunately, it doesn't work... import stripAnsi from "strip-ansi"; results in strip_ansi_1.default is not a function. If I import the function using const stripAnsi = require("strip-ansi");, it works. Not sure why it doesn't with ES6 imports.

@kevva
Copy link
Contributor

kevva commented May 31, 2017

Because CommonJS and modules works differently. It's like when you're requiring a module in CommonJS where you have to write:

const module = require('module').default;

@timkraut
Copy link
Author

timkraut commented Jun 1, 2017

I see. Is there any way that I can import this module using ES6 module syntax?

Alternatively, changing the module to:

module.exports = {
  stripAnsi: function (str) {
    return typeof str === 'string' ? str.replace(ansiRegex, '') : str;
  }
};

allows to use (in TypeScript)

import * as stripAnsi from 'strip-ansi';

as well as (in JavaScript)

const stripAnsi = require('strip-ansi');

I'm not sure if this might cause any troubles in other packages. If not, I would create a pull request for this change if you're okay with it.

@sindresorhus
Copy link
Member

We're not going to change just because you use TypeScript. If you use TypeScript it's up to you to figure out how to use packages with it. This module is exported exactly like most npm packages.

@chalk chalk locked and limited conversation to collaborators Jun 1, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants