【问题标题】:Unexpected identifier when using import abc = require('node-module')使用 import abc = require('node-module') 时出现意外标识符
【发布时间】:2019-10-05 06:13:43
【问题描述】:

我有一个 .ts 文件

import abc = require('abc'); // example
module.exports = { ...
execute(test : abc.class ,args : Array<string>) { ...

当我运行代码时,IDE 发送错误:

import abc = require('abc');
       ^^^

SyntaxError: Unexpected identifier

我不能使用 const abc = require('abc'),因为 https://imgur.com/a/hZVNdkE 并且模块 '"abc"' 没有默认的 export.ts(1192)

【问题讨论】:

  • 您正在混合使用importrequire。使用其中一种。
  • 就像@NikKyriakides 说的那样,您将imopoer 与require 混合在一起。使用它: import * as abc from 'abc'。或 const abc = require('abc')
  • 模块 '"abc"' 没有默认的 export.ts(1192)
  • 您在模块abc 中的导出效果如何?
  • ^^ import = requirevalid TS syntax,@OP:确保您的 IDE 将文件视为 Typescript,而不是 Javascript

标签: node.js typescript ecmascript-6


【解决方案1】:

在打字稿中你应该使用
import {abc} from 'abc' //if no export default

{abc} 指的是您导出的任何内容。

如果导出是默认的,您可以简单地使用:
import randomName from 'abc' //if export default

如果导出是默认的,你可以为你的导入命名。

【讨论】:

  • SyntaxError: Unexpected token {
猜你喜欢
  • 2017-09-03
  • 2018-01-05
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 2021-06-01
相关资源
最近更新 更多