Skip to content
Advertisement

Typescript compiler work but Javascript result is wrong

First of all, I want to show you my directory tree:

__ root
     __ node_modules
     __src
          __ dir1
               __ index.ts
               __ package.json
               __ deploy.config //not important here

          __ dir2 //architecture like dir1
          __ dir3
          __ ...

     __ package.json
     __ //some configuration files

I have tried to add types on my NodeJS project by adding Typescript. For that, I have installed many things into the root folder:

  1. npm i -D typescript
  2. npm i -D @types/node
  3. npm i -D @types/express

See there the index.ts script into the dir1 folder:

'use strict'

const toolPath = process.env.TOOLPATH || '';

import express, { Response, Request } from 'express';

var app = express();

app.get('/test', async (req: Request, res: Response) => {
    res.send("Hello world !");
});

module.exports = {
    app
};

To test typescript compiler I used the following command in dir1 folder: npx tsc index.ts and see the result below:

index.ts(9,28): error TS1005: ',' expected.
index.ts(9,42): error TS1005: ',' expected.
index.ts(9,54): error TS1005: ',' expected.
index.ts(10,8): error TS1005: ':' expected.
index.ts(10,30): error TS1005: ',' expected.

I do not know why I have some errors into the index.ts in

An index.js file was created but it contains errors:

'use strict';
var toolPath = process.env.TOOLPATH || '';
var express_1 = require('express');
var app = express_1["default"]();
app.get('/test', async(req, express_1.Request, res, express_1.Response), {
    res: .send("Hello world !")     //<--- ERROR THERE
});
module.exports = {
    app: app
};

Actually, I do not know why the compiler does not work but I am sure that is not a big mistake. I know that I can use a tsconfig.json file but I want to keep the actual project architecture.

Furthermore, I want to extract the index.js file into the current folder (if the script was launched into the dir1 I want to compile into dir1 folder).

I want to put a script in each package.json in each src folder to compile it and then deploy my directory into the cloud.

Advertisement

Answer

See this:

  1. npm i -D typescript
  2. npm i -D @types/node
  3. npm i -D @types/express

You have installed only types for node and express.

Fix by installing these dependencies, just like you have installed the typescript 😉

Advertisement