Import assignment cannot be used when targeting ECMAScript 2015 modules

拥有回忆 提交于 2019-12-04 09:57:08

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


Try to set module as commonjs in tsconfig

You can try this example using webpack here




回答5:


I had the same problem after updating my vsCode.

just replace with "module": "es5"

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



来源:https://stackoverflow.com/questions/39664068/import-assignment-cannot-be-used-when-targeting-ecmascript-2015-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!