SyntaxError:意外的令牌' const' - 尽管我取代了const

时间:2017-08-25 12:04:38

标签: node.js firebase webpack ecmascript-6 vue.js

我试图导出我的firebase应用程序,因此我可以在mocha测试规范中以及我尝试测试的源代码中调用它。

我的Mocha测试规范如下所示:

import Vue from 'vue'
import Edit from '@/components/Edit'
import filedata from '../../../static/filedata.js'
import submitFile from '../../../helpers/submitFile.js'
import firebaseapp from '../../../src/db.js'

var db = firebaseapp.database()
var storage = firebaseapp.storage()

describe('Edit.vue', () => {
    it('should let me add files without choosing a category', () => {
    // add files to appear on the homepage 
    var Constructor = Vue.extend(Edit)
    var vm = new Constructor().$mount()
    console.log(filedata + ' is the file data')
    var ref = storage.ref('categories')
    console.log(ref)
    submitFile(filedata)
    })  ...

submitFile文件如下所示:

var firebaseapp = require('../src/db.js')
console.log('the app is: ' + firebaseapp)
var db = firebaseapp.database()
var storage = firebaseapp.storage()



module.exports = function(files){
    // is the function being called from the test environment?
    if(files){
    console.log(files)
    }
    else {
    // function called from src -- files were null
    var files = this.$refs.upload.uploadFiles;
    }
    var storageRef = storage.ref();
    var pdfsRef = storageRef.child('files');
//    var self = this;
    console.log('the files length is ' + files.length)
    files.forEach(function(file){
    var file = file['raw'];
    var name = file['name']
    var fileref = storageRef.child(name);
    var uploadTask = fileref.put(file);
        uploadTask.then(function(snapshot){
        console.log('uploaded');
            var url = snapshot.downloadURL;
        self.gettext(url, name);
        });
    try {
        uploadTask.on('state_changed', function(snapshot){
        // Observe state change events such as progress, pause, and resume
            // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
            self.uploadProgress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
        console.log(self.uploadProgress + ' is the upload progress.');

        switch (snapshot.state) {
        case app.storage.TaskState.PAUSED: // or 'paused'
            console.log('Upload is paused');
            break;
            case app.storage.TaskState.RUNNING: // or 'running'
            console.log('Upload is running');
            break;
        }
        }, function(error) {
        // Handle unsuccessful uploads
            }, function() {
        // Handle successful uploads on complete
        // For instance, get the download URL: https://firebasestorage.googleapis.com/...
        var downloadURL = uploadTask.snapshot.downloadURL;
        });
    }
    catch(e){
        console.log(e)
    }              
    })       
}

最后,这是db.js的样子:

var Firebase = require('firebase')//import Firebase from 'firebase'
  var config = {
      ...
  };
var app = Firebase.initializeApp(config)   /// USED TO BE CONST APP
export default app

很奇怪的是,当我运行npm run unit时,我收到错误,告诉我const未被识别。

  SyntaxError: Unexpected token 'const'
  at webpack:///src/db.js~:11:0 <- index.js:38182

所以我浏览了我的db.js文件并将 const 更改为 var ,并且我得到完全相同的错误*,无论我如何改变文件。

有没有人知道会发生什么事?

这是我的package.json

{
   ...
  "private": true,
  "scripts": {
    "dev": "node build/dev-server.js",
    "start": "node build/dev-server.js",
    "build": "node build/build.js",
    "unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
    "e2e": "node test/e2e/runner.js",
    "prepare": "node ./helpers/gettestfiles.js",
    "test": "npm run prepare && npm run unit && npm run e2e && cd ../neuhold-front && npm run unit && npm run e2e",
    "deploy": "npm run build && firebase deploy"
  },
  "dependencies": {
    "babel-loader": "^7.1.2",
    "babel-preset-es2015": "^6.24.1",
    "element-ui": "^1.4.2",
    "firebase": "^4.3.0",
    "pdfjs-dist": "^1.9.450",
    "vue": "^2.3.3",
    "vue-awesome": "^2.3.1",
    "vuefire": "^1.4.3"
  },
  "devDependencies": {
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-loader": "^7.1.1",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "babel-register": "^6.22.0",
    "chalk": "^2.0.1",
    "connect-history-api-fallback": "^1.3.0",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "cssnano": "^3.10.0",
    "eventsource-polyfill": "^0.9.6",
    "express": "^4.14.1",
    "extract-text-webpack-plugin": "^2.0.0",
    "file-loader": "^0.11.1",
    "friendly-errors-webpack-plugin": "^1.1.3",
    "html-webpack-plugin": "^2.28.0",
    "http-proxy-middleware": "^0.17.3",
    "webpack-bundle-analyzer": "^2.2.1",
    "cross-env": "^5.0.1",
    "karma": "^1.4.1",
    "karma-coverage": "^1.1.1",
    "karma-mocha": "^1.3.0",
    "karma-phantomjs-launcher": "^1.0.2",
    "karma-phantomjs-shim": "^1.4.0",
    "karma-sinon-chai": "^1.3.1",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-spec-reporter": "0.0.31",
    "karma-webpack": "^2.0.2",
    "lolex": "^1.5.2",
    "mocha": "^3.2.0",
    "chai": "^3.5.0",
    "sinon": "^2.1.0",
    "sinon-chai": "^2.8.0",
    "inject-loader": "^3.0.0",
    "babel-plugin-istanbul": "^4.1.1",
    "phantomjs-prebuilt": "^2.1.14",
    "chromedriver": "^2.27.2",
    "cross-spawn": "^5.0.1",
    "nightwatch": "^0.9.12",
    "selenium-server": "^3.0.1",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "opn": "^5.1.0",
    "optimize-css-assets-webpack-plugin": "^2.0.0",
    "ora": "^1.2.0",
    "rimraf": "^2.6.0",
    "url-loader": "^0.5.8",
    "vue-loader": "^12.1.0",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.3.3",
    "webpack": "^2.6.1",
    "webpack-dev-middleware": "^1.10.0",
    "webpack-hot-middleware": "^2.18.0",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 4.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

构建/ webpack.dev.conf.js

var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge')
var baseWebpackConfig = require('./webpack.base.conf')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')

// add hot-reload related code to entry chunks
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
  baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
})

module.exports = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
  },
  // cheap-module-eval-source-map is faster for development
  devtool: '#cheap-module-eval-source-map',
  plugins: [
    new webpack.DefinePlugin({
      'process.env': config.dev.env
    }),
    // https://github.com/glenjamin/webpack-hot-middleware#installation--usage
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    // https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      inject: true
    }),
    new FriendlyErrorsPlugin()
  ]
})

表明我没有错误地编辑不同的回购...

    macbook001:docu-repo josephadmin$ cd neuhold-back
    macbook001:neuhold-back josephadmin$ cd ..
    macbook001:docu-repo josephadmin$ cd neuhold-back/src
    macbook001:src josephadmin$ ls
    App.vue     assets      components  db.js       db.js~      main.js
    macbook001:src josephadmin$ cat db.js
    /* eslint-disable */
    import Firebase from 'firebase'
      var config = {
  ...
      };
    var app = Firebase.initializeApp(config)
    export default app
    macbook001:src josephadmin$ cd ../ && npm run unit

    > neuhold-back@0.1.0 unit /Users/josephadmin/production/docu-repo/neuhold-back
    > cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run

    28 08 2017 12:45:41.353:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/
    28 08 2017 12:45:41.357:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
    28 08 2017 12:45:41.376:INFO [launcher]: Starting browser PhantomJS
    28 08 2017 12:45:42.504:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket ... with id 994129
    PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
      SyntaxError: Unexpected token 'const'
      at webpack:///src/db.js~:11:0 <- index.js:38108

    PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 ERROR (0.65 secs / 0 secs)


    PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 ERROR (0 secs / 0 secs)


    =============================== Coverage summary ===============================
    Statements   : 100% ( 0/0 )
    Branches     : 100% ( 0/0 )
    Functions    : 100% ( 0/0 )
    Lines        : 100% ( 0/0 )
    ================================================================================

    npm ERR! Darwin 15.6.0
    npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "unit"
    npm ERR! node v7.10.0
    npm ERR! npm  v4.2.0
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! neuhold-back@0.1.0 unit: `cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run`
    npm ERR! Exit status 1
    npm ERR! 
    npm ERR! Failed at the neuhold-back@0.1.0 unit script 'cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run'.
    npm ERR! Make sure you have the latest version of node.js and npm installed.
    npm ERR! If you do, this is most likely a problem with the neuhold-back package,
    npm ERR! not with npm itself.
    npm ERR! Tell the author that this fails on your system:
    npm ERR!     cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run
    npm ERR! You can get information on how to open an issue for this project with:
    npm ERR!     npm bugs neuhold-back
    npm ERR! Or if that isn't available, you can get their info via:
    npm ERR!     npm owner ls neuhold-back
    npm ERR! There is likely additional logging output above.

    npm ERR! Please include the following file with any support request:
    npm ERR!     /Users/josephadmin/.npm/_logs/2017-08-28T10_45_43_361Z-debug.log
    macbook001:neuhold-back josephadmin$ 

1 个答案:

答案 0 :(得分:3)

从抛出的错误的外观来看,这似乎是PhantomJS缓存的问题。在这里查看this Stack Overflow post