首页 > 解决方案 > 意外的令牌 { 当我需要“express-fileupload”时

问题描述

我试图让文件上传与在 Ubuntu 上运行的 NodeJS 一起工作,但我不断收到错误。添加后

const fileUpload = require('express-fileupload');

该应用程序将不再编译。它会吐出这个错误

2|theproje | /home/asgeir/nodejs/first_test_app/node_modules/express-fileupload/lib/index.js:5
2|theproje | const {buildOptions} = require('./utilities');
2|theproje |       ^
2|theproje |
2|theproje | SyntaxError: Unexpected token {
2|theproje |     at exports.runInThisContext (vm.js:53:16)
2|theproje |     at Module._compile (module.js:374:25)
2|theproje |     at Object.Module._extensions..js (module.js:417:10)
2|theproje |     at Module.load (module.js:344:32)
2|theproje |     at Function.Module._load (module.js:301:12)
2|theproje |     at Module.require (module.js:354:17)
2|theproje |     at require (internal/module.js:12:17)
2|theproje |     at Object.<anonymous> (/home/asgeir/nodejs/first_test_app/server.js:2:20)
2|theproje |     at Module._compile (module.js:410:26)
2|theproje |     at Object.Module._extensions..js (module.js:417:10)

我猜这一定是版本之间的一些兼容性问题,所以我尝试将 Node 升级到最新版本。Ubuntu 只允许我使用 v4.2.6 版本,所以我未能将其更新到更新版本。

任何有关此错误的帮助,或适用于较旧 NodeJS 版本的文件上传模块的建议将不胜感激!

标签: javascriptnode.jsexpress

解决方案


在 ubuntu 中更新到最新的 (LTS) nodejs

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs

或者,最新版本

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs

来源:https ://github.com/nodesource/distributions/blob/master/README.md


推荐阅读