Comments (9)
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.
@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.
How are you importing the module? The following works for me:
import copy = require('clipboard-copy')
copy('test')
from clipboard-copy.
from clipboard-copy.
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.
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.
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.
How about migrating the code to TypeScript?
from clipboard-copy.
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)
- Change log for 2.0.0 HOT 2
- Change README to a Promise HOT 1
- Source for empty promise rejection? HOT 1
- Thanks for this HOT 1
- Does not work on Safari HOT 2
- The npm version has the old readme HOT 1
- Copy not working in chrome version 72 HOT 2
- Copy not working on non-https dev server HOT 1
- The latest version (3.0.0) is not working in Cordova on Android 7 HOT 1
- Unhandled Promise Rejection: undefined HOT 5
- Not working in android opera browser HOT 1
- Doesn't work on Fb Messenger/Instagram in-app browser on Android device HOT 1
- Line breaks (\n) are not working on safari HOT 1
- Not working inside cross-domain iframe HOT 4
- DOMException is not supported by Edge HOT 1
- v4 breaks IE11 compatibility HOT 1
- Transpile to es5 to support older devices HOT 4
- Paste support?
- not working on Mac safari 15.0 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clipboard-copy.