Code Monkey home page Code Monkey logo

Comments (9)

LinusU avatar LinusU commented on June 20, 2024 1

The vast majority of the code on Npm uses module.exports = ... and the correct way to import them are import ... = require('...'). Depending on how you have configured TypeScript or your bundler it might work with import ... from '...' also...

When I'm using import copyToClipboard from 'clipboard-copy' I'm getting:

index.ts:1:8 - error TS1192: Module '"/private/tmp/djaskd/node_modules/clipboard-copy/index"' has no default export.

1 import copy from 'clipboard-copy'
         ~~~~


Found 1 error.

If I pass --esModuleInterop to tsc it works. Do you have esModuleInterop in your tsconfig?


You can also see the difference in the output:

import copy from 'clipboard-copy'
copy('test')

becomes:

var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var clipboard_copy_1 = __importDefault(require("clipboard-copy"));
clipboard_copy_1["default"]('test');

whilst,

import copy = require('clipboard-copy')
copy('test')

becomes

var copy = require("clipboard-copy");
copy('test');

In both cases the typings works correctly for me...

from clipboard-copy.

LinusU avatar LinusU commented on June 20, 2024 1

@gfx that wouldn't make a difference since in that case, we would do export = function clipboardCopy (text) { ... } to maintain compatibility with the current ecosystem.

Until ES modules become stable and widely used in Node.js I think that we'll stick with CommonJS style ☺️

from clipboard-copy.

LinusU avatar LinusU commented on June 20, 2024

How are you importing the module? The following works for me:

import copy = require('clipboard-copy')

copy('test')

from clipboard-copy.

LinusU avatar LinusU commented on June 20, 2024

screenshot 2018-12-11 at 14 21 20

from clipboard-copy.

feimosi avatar feimosi commented on June 20, 2024

Just like so:

import copyToClipboard from 'clipboard-copy';

and the code works fine. Actually, I've never had to use import / require structure with any other library.

from clipboard-copy.

feimosi avatar feimosi commented on June 20, 2024

Actually, I wasn't aware of esModuleInterop. As you said, if I enable it then it works fine. I'm just wondering why all the other libraries work without it and I've never had to use import ... = require() anywhere.

Some CommonJS libraries are imported in the following way though: import * as React from 'react'. Anyway, I'm not sure if it's a misuse on my side or the types could be improved to be more universal but I believe I'm not the last who comes across this issue. If there's nothing you can do, feel free to close the issue.

I'm using babel-typescript and here's my tsconfig:

{
  "module": "es2015",
  "compilerOptions": {
    "lib": ["dom", "es2015", "es2016", "es2017"],
    "baseUrl": "./src/",
    "jsx": "react",
    "target": "es5",
    "strict": true,
    "noUnusedLocals": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "noUnusedParameters": true,
    "strictNullChecks": true,
    "strictBindCallApply": true
  }
}

from clipboard-copy.

LinusU avatar LinusU commented on June 20, 2024

Here are some more info on it: https://github.com/DefinitelyTyped/DefinitelyTyped#a-package-uses-export--but-i-prefer-to-use-default-imports-can-i-change-export--to-export-default

I don't believe that there is anything we can do to make this work from our side (apart from making it an ES module which would be a breaking change).

Do you have any example of other packages that work? As far as I know, it would actually be a bug if it worked...

from clipboard-copy.

gfx avatar gfx commented on June 20, 2024

How about migrating the code to TypeScript?

from clipboard-copy.

feimosi avatar feimosi commented on June 20, 2024

Sorry for the late reply. I think we can close the issue, it's been a long time and esModuleInterop is actually recommended to be turned on so that solves any import issues.

from clipboard-copy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.