简体   繁体   English

Angular Cli-SyntaxError:“导入”和“导出”可能仅与“ sourceType:模块”一起出现

[英]Angular Cli - SyntaxError: 'import' and 'export' may appear only with 'sourceType: module'

I am trying to generate coverage report with angular cli. 我正在尝试生成带有角度cli的覆盖率报告。 however I keep getting following error. 但是我不断收到以下错误。 I am not quite sure if This is cli error or problem with my Karma.conf? 我不确定这是cli错误还是我的Karma.conf有问题? ng test / npm test Runs fine... So something to do with code-coverage option? ng test / npm test运行正常...与代码覆盖选项有关吗?

    Error: Module build failed: SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (3:0)
        at Parser.pp$5.raise (C:\Development\source\\node_modules\babylon\lib\index.js:4443:13)
        at Parser.pp$1.parseStatement (C:\Development\source\\node_modules\babylon\lib\index.js:1875:16)
        at Parser.parseStatement (C:\Development\source\\node_modules\babylon\lib\index.js:5795:22)
        at Parser.pp$1.parseBlockBody (C:\Development\source\\node_modules\babylon\lib\index.js:2262:21)
        at Parser.pp$1.parseTopLevel (C:\Development\source\\node_modules\babylon\lib\index.js:1772:8)
        at Parser.parse (C:\Development\source\\node_modules\babylon\lib\index.js:1667:17)
        at Object.parse (C:\Development\source\\node_modules\babylon\lib\index.js:7163:37)
        at Instrumenter.instrumentSync (C:\Development\source\\node_modules\istanbul-lib-instrument\dist\instrumenter.js:121:31)
        at Instrumenter.instrument (C:\Development\source\\node_modules\istanbul-lib-instrument\dist\instrumenter.js:176:32)
        at Object.module.exports (C:\Development\source\\node_modules\istanbul-instrumenter-loader\index.js:25:25),Module build failed: SyntaxError: 'import
    ' and 'export' may appear only with 'sourceType: module' (2:0)
        at Parser.pp$5.raise (C:\Development\source\\node_modules\babylon\lib\index.js:4443:13)
        at Parser.pp$1.parseStatement (C:\Development\source\\node_modules\babylon\lib\index.js:1875:16)
        at Parser.parseStatement (C:\Development\source\\node_modules\babylon\lib\index.js:5795:22)
        at Parser.pp$1.parseBlockBody (C:\Development\source\\node_modules\babylon\lib\index.js:2262:21)
        at Parser.pp$1.parseTopLevel (C:\Development\source\\node_modules\babylon\lib\index.js:1772:8)
        at Parser.parse (C:\Development\source\\node_modules\babylon\lib\index.js:1667:17)
        at Object.parse (C:\Development\source\\node_modules\babylon\lib\index.js:7163:37)
        at Instrumenter.instrumentSync (C:\Development\source\\node_modules\istanbul-lib-instrument\dist\instrumenter.js:121:31)
        at Instrumenter.instrument (C:\Development\source\\node_modules\istanbul-lib-instrument\dist\instrumenter.js:176:32)
        at Object.module.exports (C:\Development\source\\node_modules\istanbul-instrumenter-loader\index.js:25:25)
        at Compiler.compiler.plugin (C:\Development\source\\node_modules\@angular\cli\plugins\karma-webpack-throw-error.js:10:23)
        at Compiler.applyPlugins (C:\Development\source\\node_modules\tapable\lib\Tapable.js:61:14)
        at Watching._done (C:\Development\source\\node_modules\webpack\lib\Compiler.js:91:17)
        at onCompiled (C:\Development\source\\node_modules\webpack\lib\Compiler.js:50:17)
        at C:\Development\source\\node_modules\webpack\lib\Compiler.js:480:13
        at next (C:\Development\source\\node_modules\tapable\lib\Tapable.js:138:11)
        at Compiler.<anonymous> (C:\Development\source\\node_modules\webpack\lib\CachePlugin.js:62:5)
        at Compiler.applyPluginsAsyncSeries (C:\Development\source\\node_modules\tapable\lib\Tapable.js:142:13)
        at C:\Development\source\\node_modules\webpack\lib\Compiler.js:477:10
        at Compilation.applyPluginsAsyncSeries (C:\Development\source\\node_modules\tapable\lib\Tapable.js:131:46)
        at self.applyPluginsAsync.err (C:\Development\source\\node_modules\webpack\lib\Compilation.js:640:19)
        at next (C:\Development\source\\node_modules\tapable\lib\Tapable.js:138:11)
        at Compilation.<anonymous> (C:\Development\source\\node_modules\webpack\lib\ProgressPlugin.js:122:5)
        at Compilation.applyPluginsAsyncSeries (C:\Development\source\\node_modules\tapable\lib\Tapable.js:142:13)
        at self.applyPluginsAsync.err (C:\Development\source\\node_modules\webpack\lib\Compilation.js:631:11)
        at next (C:\Development\source\\node_modules\tapable\lib\Tapable.js:138:11)

My karma.conf looks like this 我的karma.conf看起来像这样

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('karma-chrome-launcher'),
      require('@angular/cli/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    files: [
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['@angular/cli']
    },
    mime: {
      'text/x-typescript': ['ts', 'tsx']
    },
    coverageIstanbulReporter: {
      reports: ['html', 'lcovonly', 'json'],
      fixWebpackSourcePaths: true
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
      ? ['progress', 'coverage-istanbul']
      : ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

I hit the same issue today, the fix in my case was a change to the tsconfig.spec.json file. 我今天遇到了同样的问题,我的问题是对tsconfig.spec.json文件的更改。 I changed the module entry from "module": "es2015" to "module": "commonjs" 我将模块条目从"module": "es2015"更改为"module": "commonjs"

This resolved the compilation error running ng test --cc and created the expected coverage report. 这解决了运行ng test --cc的编译错误,并创建了预期的覆盖率报告。 Hope this helps! 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模块构建失败:SyntaxError:&#39;import&#39;和&#39;export&#39;可能只出现&#39;sourceType:&#39;模块“&#39; - Module build failed: SyntaxError: 'import' and 'export' may appear only with 'sourceType: “module”' 离子2-“导入”和“导出”可能仅与“ sourceType:module”一起出现 - Ionic 2 - 'import' and 'export' may appear only with 'sourceType: module' Ng-Lightning ParseError“导入”和“导出”可能仅与“ sourceType:模块”一起出现 - Ng-Lightning ParseError 'import' and 'export' may appear only with 'sourceType: module' 我如何解决 ParseError: &#39;import&#39; 和 &#39;export&#39; 可能只出现在 &#39;sourceType: module&#39; 导入 UpgradeAdapter 时? - How do I solve ParseError: 'import' and 'export' may appear only with 'sourceType: module' when importing UpgradeAdapter? 语法错误:导出声明​​只能出现在模块的顶层 - SyntaxError: export declarations may only appear at top level of a module 模块解析失败:“导入”和“导出”可能只出现在顶层 - Module parse failed: 'import' and 'export' may only appear at the top level “进口”和“出口”可能仅出现在顶层-角钢品牌是全新的 - 'import' and 'export' may only appear at the top level - brand new to angular 导入和导出可能仅出现在顶层 - import and export may only appear at the top level “导入”和“导出”可能只出现在顶层。 但它处于顶层和结束。 (盖茨比和 React JS) - Import' and 'export' may only appear at the top level. But it is at the top level and end. (Gatsby & React JS) crypto js:全局模块导出可能仅在顶级角度键入中出现 - crypto js :global module exports may only appear at top level angular typings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM