-1

I have an angular app running fine. Next, I need to write some rest services, so I thought I will create a node.js server inside the same angular app as it will be just couple of APIs. I created a file at src\server\server.ts and following is its content -

import express from 'express';
const app = express();
const port = 3000;
app.get('/', (req, res) => {
  res.send('The sedulous hyena ate the antelope!');
});
app.listen(port, err => {
  if (err) {
    return console.error(err);
  }
  return console.log(`server is listening on ${port}`);
});

I get an error at line 1 itself on express -

import express
Module '"c:/Users/A12345/quiz-master/node_modules/@types/express/index"' can only be default-imported using the 'allowSyntheticDefaultImports' flagts(1259)
index.d.ts(107, 1): This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.

I am not getting as what is causing it. Following is my package.json -

{
  "name": "quiz-master",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "server": "tsc && node src/server/server.ts",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~8.2.14",
    "@angular/common": "~8.2.14",
    "@angular/compiler": "~8.2.14",
    "@angular/core": "~8.2.14",
    "@angular/forms": "~8.2.14",
    "@angular/platform-browser": "~8.2.14",
    "@angular/platform-browser-dynamic": "~8.2.14",
    "@angular/router": "~8.2.14",
    "@fortawesome/fontawesome-free": "^5.12.0",
    "@types/chart.js": "^2.9.5",
    "angular-bootstrap-md": "^8.8.0",
    "animate.css": "^3.7.2",
    "chart.js": "^2.5.0",
    "core-js": "^2.5.4",
    "express": "^4.17.1",
    "hammerjs": "^2.0.8",
    "pg": "^7.14.0",
    "pg-hstore": "^2.3.3",
    "prismjs": "^1.17.1",
    "rxjs": "~6.5.3",
    "sequelize": "^5.21.3",
    "sequelize-typescript": "^1.1.0",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.803.20",
    "@angular/cli": "~8.3.20",
    "@angular/compiler-cli": "~8.2.14",
    "@angular/language-service": "~8.2.14",
    "@types/express": "^4.17.2",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.5.3"
  }
}

I referred the following links - link1 and link2

a p
  • 1,121
  • 6
  • 26
  • 51
  • Have you tried setting the ```allowSyntheticDefaultImports``` flag to true? – nll_ptr Dec 17 '19 at 13:29
  • No, but why do we need that? – a p Dec 17 '19 at 13:30
  • ```This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.``` The error seems pretty clear. – nll_ptr Dec 17 '19 at 13:32
  • Yeah, it seems pretty clear, but just wanted to know the reason behind it. Anyways, I did try that and this is what I get - `(function (exports, require, module, __filename, __dirname) { import express from 'express'; ^^^^^^^ SyntaxError: Unexpected identifier at new Script (vm.js:79:7)` – a p Dec 17 '19 at 13:33
  • Well, someone marked it negative rather than helping. – a p Dec 17 '19 at 13:35
  • Hmm okay. allowSyntheticDefaultImports just allows importing from modules that have no default exports such as your server.js file. This seems like an issue with how node imports modules. Take a look at https://medium.com/@stephenfluin/adding-a-node-typescript-backend-to-your-angular-app-29b0e9925ff, it seems that you have to set up an independent tsconfig file for your server. – nll_ptr Dec 17 '19 at 13:45

1 Answers1

0

I got my answer here -

import * as express from 'express'; to const express = require('express');

a p
  • 1,121
  • 6
  • 26
  • 51