2
votes

I have followed various thread here but I can't get it to work. I'd like to compile the following code in typescript. This code come from a single js file, but since my problems come from modle inclusion I'm compiling just this:

import tweetnacl from "./node_modules/tweetnacl/nacl"; 
console.log(`here we are`);

What I have tried: Using this tsconfig.json

{
  "compilerOptions": {
    "target":"ES6",
    "moduleResolution": "classic",
  }
}

Compiling with tsc --module es6 --target es6 and secify every option for module npm install @types/node also did not worked for me.

I run tsc --module commonjs test.ts and get

node_modules/tweetnacl/nacl.d.ts(3,1): error TS1128: Declaration or statement expected.
node_modules/tweetnacl/nacl.d.ts(3,11): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(3,25): error TS1005: '{' expected.
node_modules/tweetnacl/nacl.d.ts(21,81): error TS1110: Type expected.
node_modules/tweetnacl/nacl.d.ts(22,9): error TS1131: Property or signature expected.
node_modules/tweetnacl/nacl.d.ts(22,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(23,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(24,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(30,9): error TS1131: Property or signature expected.
node_modules/tweetnacl/nacl.d.ts(30,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(31,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(32,5): error TS1128: Declaration or statement expected.
node_modules/tweetnacl/nacl.d.ts(36,110): error TS1110: Type expected.
node_modules/tweetnacl/nacl.d.ts(37,86): error TS1110: Type expected.
node_modules/tweetnacl/nacl.d.ts(52,9): error TS1131: Property or signature expected.
node_modules/tweetnacl/nacl.d.ts(52,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(53,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(54,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(55,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(56,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(57,5): error TS1128: Declaration or statement expected.
node_modules/tweetnacl/nacl.d.ts(74,74): error TS1110: Type expected.
node_modules/tweetnacl/nacl.d.ts(77,9): error TS1131: Property or signature expected.
node_modules/tweetnacl/nacl.d.ts(77,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(78,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(79,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(80,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(81,5): error TS1128: Declaration or statement expected.
node_modules/tweetnacl/nacl.d.ts(85,9): error TS1131: Property or signature expected.
node_modules/tweetnacl/nacl.d.ts(85,18): error TS1005: ';' expected.
node_modules/tweetnacl/nacl.d.ts(86,5): error TS1128: Declaration or statement expected.
node_modules/tweetnacl/nacl.d.ts(87,1): error TS1128: Declaration or statement expected.

How can I get node module work in typescript? Thanks in advance

EDIT: After updating tsc

tsc --v
Version 3.0.1

I got this error

test.ts:1:8 - error TS1192: Module '"./node_modules/tweetnacl/nacl"' has no default export.

1 import tweetnacl from "tweetnacl";
     ~~~~~~~~~
1

1 Answers

1
votes

Assuming that your node_modules/tweetnacl/nacl.d.ts file looks like this, based on the line and column numbers of the errors, it looks like TypeScript is having trouble parsing multiple language constructs that were added in TypeScript 2.0. If you are using a version of TypeScript older than 2.0 (two years old as of this writing!), try upgrading.

Round 2: The tweetnacl module indeed has no default export. It looks like the import should be:

import * as tweetnacl from "./node_modules/tweetnacl/nacl";