Search code examples
node.jstypescriptts-nodepolkadot-jsts-node-dev

getting an error "SyntaxError: Unexpected string" near the import statement working with ts-node-dev


i have trying to use typescript libraries with node but I keep getting these SyntaxErrors. i am running the dev script form the package.json using the ts-node-dev. the error show near the import statements and i am not sure if its a problem with my tsconfig.json ( it is also attached below). But i am using "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

package.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/**/*"
  ]
}

Solution

  • For anyone having this issue , this was a problem with the node.js version i was using , i was using something older than 10, when updated the version to 14+, the issue was solved.