首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在typescript中导入hashids时出现异常

在typescript中导入hashids时出现异常
EN

Stack Overflow用户
提问于 2020-03-13 23:34:23
回答 1查看 430关注 0票数 0

我正在尝试在Typescript中导入hashids

您可以从here克隆代码

= index.ts =

代码语言:javascript
复制
import Hashids from "hashids";
const encoder = new Hashids();

但是我得到了下一个错误:

=控制台=

代码语言:javascript
复制
export { Hashids as default };
^^^^^^
SyntaxError: Unexpected token 'export'
    at Module._compile (internal/modules/cjs/loader.js:895:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/var/www/persona-service/src/Example.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Module.m._compile (/var/www/persona-service/node_modules/ts-node/src/index.ts:814:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:995:10)

这是我的tsconfig.json

代码语言:javascript
复制
{
  "compilerOptions": {
    "incremental": true,
    "moduleResolution": "node",
    "module": "CommonJS",
    "esModuleInterop": true,
    "target": "es6",
    "types": [
      "node",
      "express",
      "hashids"
    ]
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

我还通过以下配置使用了nodemon

代码语言:javascript
复制
{
  "watch" : ["src"],
  "ext": "ts",
  "exec": "ts-node ./src/index.ts"
}

这里会发生什么呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-14 00:41:07

检查hashids包的git代码库,发现与某些节点版本的导入相关的问题:

hashids issue on the repo

这里提到的一种解决方法是使用require而不是import

代码语言:javascript
复制
const Hashids = require('hashids/cjs');

我希望这能让你重回正轨。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60673277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档