【问题标题】:How to solve unexpected token import如何解决意外的令牌导入
【发布时间】:2019-09-26 15:55:36
【问题描述】:

下面是我的代码

import * as errorHandler from "errorhandler";

import { default as app } from "./express";
import * as cors from "cors";
/**
 * Error Handler. Provides full stack - remove for production
 */
app.use(errorHandler());

我遇到了如下所示的错误

(function (exports, require, module, __filename, __dirname) { import * as errorHandler from "errorhandler"; SyntaxError:意外的令牌导入

如果能为此解决方案提供一些帮助,我将不胜感激

【问题讨论】:

  • 目前你不能在 Node.js 中直接使用ES6 模块系统语法(import/export)。要么使用答案之一中提到的实验性支持。或者使用像 babel More info 这样的转译器,它基本上将此代码转换为 commonJS 模块语法。

标签: typescript ecmascript-6


【解决方案1】:

Node.js 默认不支持 ECMAScript 样式的模块(import 就是这样)。使用--experimental-modules 运行您的应用程序,或改为使用require()。更多信息可以在in the official documentation找到。

【讨论】:

    猜你喜欢
    • 2021-10-02
    • 2016-06-18
    • 2020-07-24
    • 2019-03-21
    • 2020-03-16
    • 2020-05-07
    • 1970-01-01
    • 2017-05-20
    • 2019-08-07
    相关资源
    最近更新 更多