【问题标题】:ES6 import syntax in Node.js app with Babel and Typescript带有 Babel 和 Typescript 的 Node.js 应用程序中的 ES6 导入语法
【发布时间】:2019-07-29 17:33:22
【问题描述】:

index.ts:

import express from "express"
const app = express()
app.listen(3000, () => console.log("Example app listening on port 3000!!"))

.babelrc:

{
    "presets": ["@babel/preset-typescript", "@babel/preset-env"],
    "plugins": [
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-proposal-object-rest-spread"
    ]
}

命令npx babel-node src/index.ts报错:

(function (exports, require, module, __filename, __dirname) { import express from "express"
                                                                     ^^^^^^^
SyntaxError: Unexpected identifier

但是,如果我将 index.ts 重命名为 index.js 并运行命令 npx babel-node src/index.js,那么它可以正常工作。

或者,如果我将 index.ts 的第一行更改为 const express = require("express"),那么该命令(对于 index.ts)也可以正常工作。

所以看起来我可以使用 ES6 导入语法或使用 Typescript,但不能同时使用。

如果可能,如何解决这个问题?

安装了以下模块:

"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/node": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-typescript": "^7.3.3",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
"tslint": "^5.18.0",
"typescript": "^3.5.3"
"@types/express": "^4.17.0",
"express": "^4.17.1",

【问题讨论】:

    标签: node.js typescript babeljs


    【解决方案1】:

    试试Webpack-Generator babel 和 Typescript 的组合。它提供了 ES6 和 TypeScript 配置的所有要求设置。

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 2019-10-20
      • 2016-10-26
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多