Ionic 2 - 'import'和'export'可能只出现'sourceType:module'

时间:2022-11-28 23:01:18

Working on a new project with a very fresh install of ionic2 ,after install angular2-jwt I am getting this error:

使用新安装的ionic2处理新项目,安装angular2-jwt后我收到此错误:

ParseError: 'import' and 'export' may appear only with 'sourceType: module'

D:\desenv\arquivos\workspace_inbit\medipop-parent\medipop-app\node_modules\angular2-jwt\angular2-jwt.ts:1
import {Injectable, Injector} from 'angular2/core';

To reproduce:

重现:

ionic start testapp --v2 --ts 
cd testapp
npm i --save angular2-jwt

and the app page:

和应用页面:

@App({
    templateUrl: 'build/app.html',
    providers: [
        provide(AuthHttp, {
            useFactory: (http) => {
                return new AuthHttp(new AuthConfig({
                    headerPrefix: '',
                    noJwtError: true
                }), http);
            },
            deps: [Http]
        })
    ]
})
class MyApp {}

Does anyone have a clue of how to solve this little puzzle?

有没有人知道如何解决这个小谜题?

3 个解决方案

#1


1  

Try to add this line to your ts config:

尝试将此行添加到您的ts配置:

"parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module"
},

If this doesn't work, try with this config wich is working on my computer (tsconfig.json):

如果这不起作用,请尝试使用此配置,这是在我的计算机上工作(tsconfig.json):

{
    "compilerOptions": {
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "ES5",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "declaration": true,
        "moduleResolution":"node"
    },
    "files": [
        "angular2-jwt.ts",
        "typings/browser.d.ts",
        "custom.d.ts"
    ]
}

#2


0  

'import' and 'export' may appear only with 'sourceType: module'

'import'和'export'可能只出现'sourceType:module'

You need to specify a module in your tsconfig or the command line. e.g.

您需要在tsconfig或命令行中指定模块。例如

'module': 'commonjs'

More

https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

#3


0  

I had the same issue today on Ionic 2 beta 11 and for me it was coz of a (.js) extension file(user-data.js) instead of (.ts) file (user-data.ts) , so changing .js file extension to .ts solved my issue.

我今天在Ionic 2 beta 11上遇到了同样的问题,对我而言,它是一个(.js)扩展文件(user-data.js)而不是(.ts)文件(user-data.ts)的coz,所以改变了。 js文件扩展名为.ts解决了我的问题。

#1


1  

Try to add this line to your ts config:

尝试将此行添加到您的ts配置:

"parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module"
},

If this doesn't work, try with this config wich is working on my computer (tsconfig.json):

如果这不起作用,请尝试使用此配置,这是在我的计算机上工作(tsconfig.json):

{
    "compilerOptions": {
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "ES5",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "declaration": true,
        "moduleResolution":"node"
    },
    "files": [
        "angular2-jwt.ts",
        "typings/browser.d.ts",
        "custom.d.ts"
    ]
}

#2


0  

'import' and 'export' may appear only with 'sourceType: module'

'import'和'export'可能只出现'sourceType:module'

You need to specify a module in your tsconfig or the command line. e.g.

您需要在tsconfig或命令行中指定模块。例如

'module': 'commonjs'

More

https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

#3


0  

I had the same issue today on Ionic 2 beta 11 and for me it was coz of a (.js) extension file(user-data.js) instead of (.ts) file (user-data.ts) , so changing .js file extension to .ts solved my issue.

我今天在Ionic 2 beta 11上遇到了同样的问题,对我而言,它是一个(.js)扩展文件(user-data.js)而不是(.ts)文件(user-data.ts)的coz,所以改变了。 js文件扩展名为.ts解决了我的问题。