42

I'm trying to use the follwing line:

import Clipboard = require('clipboard');

and I get the following error:

   [default] c:\xampp\htdocs\isitperfect\node_modules\angular2-clipboard\src\clipboard.directive.ts:2:0 
Import assignment cannot be used when targeting ECMAScript 2015 modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.

The error is in this line:

import Clipboard = require('clipboard');

I tried:

import * as Clipboard from 'clipboard';

and some other variations but couldn't figure out how to fix it.

I'm using typescript 2.0.0

Any ideas?

maxisam
  • 21,975
  • 9
  • 75
  • 84
TheUnreal
  • 23,434
  • 46
  • 157
  • 277

7 Answers7

52

I was facing the same issue as you.

In the tsconfig.json file I replaced:

"module": "es6"

with

"module": "commonjs"

and restarted the terminal. It worked.

Tsvetan Ganev
  • 8,246
  • 4
  • 26
  • 43
Shashikant Pandit
  • 2,752
  • 22
  • 29
12

I had the same problem and changing to:

import * as myGlobals from "../globals";

fixed the problem. globals.ts file is in the main app folder, and I'm loading it up from subfolder services.

Pipo
  • 4,653
  • 38
  • 47
KrystianC
  • 422
  • 3
  • 13
6

In case you are using Angular CLI: I was able to compile my project after I had set the value of module to es2016 in the file src/tsconfig.app.json.

Leukipp
  • 550
  • 2
  • 9
  • 25
  • 2
    es2016 does not seem to be a valid value for the module field. https://www.typescriptlang.org/docs/handbook/compiler-options.html – Lyn Headley Oct 12 '17 at 18:55
  • it's `lib`, and not `module` – dev27 May 10 '18 at 22:41
  • 1
    TypeScript gave me into trouble for using `es2016` as the module setting, but suggested `esnext` as an alternative and now it is happy. – Eraph Jun 26 '18 at 23:12
2

I used default member of imported function.

import {default as Clipboard} from 'clipboard';
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
1

Try to set module as commonjs in tsconfig

You can try this example using webpack here

maxisam
  • 21,975
  • 9
  • 75
  • 84
  • 1
    disclaimer: I don't maintain the webpack solution anymore. But I have updated the broken link for you – maxisam Jan 01 '19 at 17:03
0

I had the same problem after updating my vsCode.

just replace with "module": "es5"

to "module": "commonjs" in tsconfig.app.json

rcanpahali
  • 2,565
  • 2
  • 21
  • 37
0

Similar to KristianC, I got this warning for lodash on upgrade to Angular 11

const cloneDeep = require('lodash-es/cloneDeep');

"Import assignment cannot be used when targeting ECMAScript modules."

Warning resolved on updating "require" to "import"

import { cloneDeep } from 'lodash-es';
Alex Cooper
  • 475
  • 3
  • 7
  • 18