SyntaxError: Unexpected token 'const' -- 尽管我替换了 const
Posted
技术标签:
【中文标题】SyntaxError: Unexpected token \'const\' -- 尽管我替换了 const【英文标题】:SyntaxError: Unexpected token 'const' -- despite the fact that I replaced constSyntaxError: Unexpected token 'const' -- 尽管我替换了 const 【发布时间】:2018-02-03 11:34:36 【问题描述】:我正在尝试导出我的 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()
]
)
为了表明我没有错误地编辑不同的 repos ...
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$
【问题讨论】:
重建应用,你似乎使用了一些缓存机制 @AlexandruOlaru 你的意思是npm run build
?我试过了,结果是一样的
问题在于 webpack 设置.. 分享一下
你重启你的应用了吗?
@Jehy 你到底是什么意思?
【参考方案1】:
从抛出的错误来看,这似乎是 PhantomJS 缓存的问题。在这里查看this Stack Overflow post。
【讨论】:
以上是关于SyntaxError: Unexpected token 'const' -- 尽管我替换了 const的主要内容,如果未能解决你的问题,请参考以下文章
求教php运行时出错:Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION 。。。
Import Unexpected identifier + SyntaxError: Unexpected string
js动态加载的a标签的onclick事件参数传递,一直报Uncaught SyntaxError: Invalid or unexpected token