1

我试图将 typescript 库与 node 一起使用,但我不断收到这些 SyntaxErrors。我正在使用 ts-node-dev 从 package.json 运行开发脚本。错误显示在导入语句附近,我不确定我的 tsconfig.json 是否有问题(它也附在下面)。但我正在使用 "module":"commonjs" 。

[INFO] 13:22:15 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.4.3)
/mnt/d/Polkadex/web-proxy/node_modules/@polkadot/keyring/index.js:3
import "./detectPackage.js";
       ^^^^^^^^^^^^^^^^^^^^ 

SyntaxError: Unexpected string
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Module._compile (/mnt/d/Polkadex/web-proxy/node_modules/source-map-support/source-map-support.js:568:25)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.nodeDevHook [as .js] (/mnt/d/Polkadex/web-proxy/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/mnt/d/Polkadex/web-proxy/src/helpers/testUser.ts:1:1)
[ERROR] 13:22:19 SyntaxError: Unexpected string

包.json

{
  "name": "web-proxy",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "@polkadot/api": "^6.0.3",
    "@polkadot/keyring": "^7.4.1",
    "@polkadot/util": "^7.4.1",
    "@polkadot/util-crypto": "^7.4.1",
    "axios": "^0.21.4",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "typescript": "^4.4.3"
  },
  "devDependencies": {
    "@polkadot/typegen": "^6.0.3",
    "@types/axios": "^0.14.0",
    "@types/express": "^4.17.13",
    "ts-node": "^10.2.1",
    "ts-node-dev": "^1.1.8"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "generate:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package sample-polkadotjs-typegen/interfaces --input ./src/interfaces",
    "dev": "ts-node-dev --respawn --pretty --transpile-only src/index.ts"
  },
  "author": "",
  "license": "ISC"
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
  },
  "exclude": [
    "./node_modules"
  ],
  "include": [
    "./src/**/*"
  ]
}
4

2 回答 2

1

对于遇到此问题的任何人,这是我使用的 node.js 版本的问题,我使用的版本早于 10,当将版本更新到 14+ 时,问题已解决。

于 2021-09-26T13:37:45.733 回答
0

您正在导入模块import "./detectPackage.js";,这不是正确的方法。

以这种方式导入您的模块:

import SomeModule from "./detectPackage.js";
于 2021-09-21T09:00:01.193 回答