我正在尝试在Typescript中导入hashids:
您可以从here克隆代码
= 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"
}这里会发生什么呢?
发布于 2020-03-14 00:41:07
检查hashids包的git代码库,发现与某些节点版本的导入相关的问题:
这里提到的一种解决方法是使用require而不是import
const Hashids = require('hashids/cjs');我希望这能让你重回正轨。
https://stackoverflow.com/questions/60673277
复制相似问题