Исключение при импорте хешей в машинописи - PullRequest
0 голосов
/ 13 марта 2020

Я пытаюсь импортировать hashids в Typescript:

Вы можете клонировать код из здесь

========== index.ts ==========

import Hashids from "hashids";
const encoder = new Hashids();

Но я получаю следующую ошибку:

========== Консоль === ======

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

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

Я также использую nodemon с этой конфигурацией:

{
  "watch" : ["src"],
  "ext": "ts",
  "exec": "ts-node ./src/index.ts"
}

Что здесь может происходить?

1 Ответ

1 голос
/ 13 марта 2020

Проверка репо git для пакета hashids обнаружила проблему, связанную с импортом для определенных версий узлов:

Проблема с хэшами в репо

Упомянутое обходное решение вместо импорта нужно использовать require

const Hashids = require('hashids/cjs');

Надеюсь, это вернет вас в нужное русло.

...