【发布时间】:2018-03-02 08:15:51
【问题描述】:
我正在尝试将旧的 nodejs(版本 6.x)rest 服务项目迁移到最新的 nodejs(版本 9.6.1)rest 服务项目,并希望在没有任何转换器的情况下遵循 ECMAScript 6 规范。
当前项目使用 express、body-parser、moment、multer、cors、ethereumjs-tx、secp256k1、requirejs 和其他一些 nodejs 模块,所以我的问题是,所有这些不同的模块以及 nodejs 是否都支持 ECMAScript 6 规范本地人。
在互联网上,我看到很多使用 bable 转换器的解决方案,但我不想遵循这种方法。
我再次发现 https://github.com/mench/express-restful-es6 是一个 ECMAScript 6 的 express 实现,但它再次不起作用。它在默认导入和使用 @ 装饰时失败。
ECMAScript 6 对 node.js 的支持也是如此,它的模块已经实现,或者我应该等待一段时间。
编辑
我同意 ECMAScript 6 中不支持装饰器模式,但导入和导出也不起作用,我尝试运行以下代码...
import express from 'express'
import restful from 'express-restful-es6'
var server = express()
console.log(restful)
restful.configure(server,{
dirname: '/projectlocation' + '/resources'
})
server.listen(9000)
console.log("Listening on port 9000 .....")
但出现以下错误...
> ethereum-rest-api@1.0.0 start /projectlocation/ethereum-rest-api
> /nodelocation/bin/node --experimental-modules index.mjs
(node:31753) ExperimentalWarning: The ESM module loader is experimental.
{ Rest: [Function: Rest],
middleware: [Function: middleware],
default: Restful {} }
TypeError: restful.configure is not a function
at file:///projectlocation/index.mjs:7:9
at ModuleJob.run (internal/loader/ModuleJob.js:102:14)
at <anonymous>
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project@1.0.0 start: `/nodelocation/bin/node --experimental-modules index.mjs`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
再次@lorefnon,如果模块不支持 ECMAScript 6 样式导入/导出,我是否可以在我的代码中导入它,例如,如果我尝试使用最新的 expression.js,我可以使用 ECMAScript 6 语法导入它而不任何转换器。
【问题讨论】:
-
看来模块正在使用decorators。那不是 ES6 的功能。我相信这是提议的未来功能(超越 ES6),您要么需要一个支持装饰器的环境,要么需要对其进行转译。
-
你能展示一下你想在这个库中使用什么代码吗?
-
当前 8.x 版本的 node.js 确实很好地支持了 ES6,甚至支持了许多 ES7 特性。您不必等待 ES6 支持。另一方面,装饰器不是 ES6 特性。
-
@jfriend00 2 请检查我更新的帖子我添加了一些代码和错误消息。
-
如果您在导入和导出方面遇到问题,为什么您接受一个根本没有解决该问题的答案?或者,问题实际上不是由导入/导出引起的吗?
标签: javascript node.js express ecmascript-6