0

I am getting an exception while importing module

enter image description here

Here is code

import * as express from 'express';

export class Server {

    app: express.Application
    constructor() {
        const port = 3000 || process.env.PORT
        this.app = express()
        this.app.listen(port, () => {
            console.log(`Listening on Port: ${port}`)
        })
    }
}

export default new Server()

Note: I am creating API with Angular 7, both client & server apps have common node_modules and other configuration files like package.json and angular.json

The same code, I am running with Angular 6 is working fine. Is there any configuration issue?

Update 1:

Old Angular 6 project, I created with Node v8.9.3 and new Angular 7 project with Node v10.14.2 and facing issue that server code is running with old project but giving stated error with new project.

Update 2:

When I changed it to

var express = require("express");

const app = express()
const port = 3001 || process.env.PORT
app.listen(port, () => {
    console.log(`Listening on Port: ${port}`)
})

module.exports = app;

It worked fine... but I need to use ES6 syntax

WasiF
  • 26,101
  • 16
  • 120
  • 128
  • Is this the whole code of the Server ? – Joniras Dec 19 '18 at 06:09
  • Yes, it is the complete code – WasiF Dec 19 '18 at 06:12
  • I dont see anything wrong with this part of the code. Are you sure this part is causing the compilation to fail ? – Joniras Dec 19 '18 at 06:25
  • this code is working fine with `Angular 6` but not with `Angular 7`. Is there any compatibility or syntax issue – WasiF Dec 19 '18 at 06:28
  • This code has nothing to do with Angular, thats Node. Did you change your node version ? – Joniras Dec 19 '18 at 06:35
  • Yes, it is now `Node v10.14.2` and `npm v6.4.1` – WasiF Dec 19 '18 at 06:36
  • Please provide that information as well as the previous version in your question. Also Maybe the Snippet where you import/use the Server (as you seem to import it at some point) – Joniras Dec 19 '18 at 06:45
  • Both apis have same code just copied and pasted from old project. What I need to provide to the answer i.e. `package.json`, `angular.json` or else. – WasiF Dec 19 '18 at 06:49
  • Just state what version of node you had before and after. Maybe this helps. And the part of the code where ou import the Server. – Joniras Dec 19 '18 at 07:12
  • just running this file with `nodemon --exec ts-node ./api/server.ts`, nowhere server is being imported. Update question, please review – WasiF Dec 19 '18 at 07:26

2 Answers2

0

I think i figured it out:

Node does not allow ECMAScriptModules yet (experimental)

So instead of this:

import * as express from "express";

you should use this:

var express = require("express");

But you can use the flag --experimental-modules to make it compile (its not stable yet).

See this Question

Joniras
  • 1,258
  • 10
  • 32
  • same command is being used to run both the applications. It's a standard command used by professional – WasiF Dec 19 '18 at 07:37
  • then why this ES6 syntax is supporting the previous project while it have previous packages installed than new project have latest packages installed – WasiF Dec 19 '18 at 08:16
  • Maybe it got compiled with babel ? (that was a possibility) or also this command – Joniras Dec 19 '18 at 08:18
  • How to check with what it is being compiled? – WasiF Dec 19 '18 at 08:20
  • 1
    the commands with whom you started the server determine the way it got compiled.(babel /src or node /src) – Joniras Dec 19 '18 at 08:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185478/discussion-between-joniras-and-wasif). – Joniras Dec 19 '18 at 10:21
0

It could be problem in tsconfig.json. Check compilerOptions.module. Example:

   {
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}
Alex M.
  • 129
  • 5
  • This isn't a pertaining to Angular. It's an express js application used along with Angular. – Vinit Sarvade Dec 19 '18 at 18:03
  • @Vinit Sarvade I dont write about Angular here). I use typescript in server side and sometimes problem is in a bad config. Please explain me in more detail your comment... – Alex M. Dec 19 '18 at 18:20
  • This isn't a problem of bad config. You need to also install typings for your express application. Please install the following `npm i -D @types/express` and then modify the import as `import express from 'express'` – Vinit Sarvade Dec 19 '18 at 18:28
  • Also make sure you are running the application using `tsc` command. Assuming that your server code is in server.ts, run it using `tsc server.ts` – Vinit Sarvade Dec 19 '18 at 18:31
  • @Vinit Sarvade If **target** will be not **es5** you will get this error. Because in output will be **import** and nodejs don't understant it without flag --experimental-modules yet. – Alex M. Dec 19 '18 at 18:37
  • If you running your server with `tsc` you don't need to specify the target as it will directly run typescript. – Vinit Sarvade Dec 19 '18 at 18:43
  • @Vinit Sarvade :) Yes i use tsc but with my specific config – Alex M. Dec 19 '18 at 18:49
  • @MrM Thank you for your answer, I changed my code to `"module": "esnext"` but still error is there. If you believe you can solve this please test this source code [https://github.com/wasifnaeem/Test](https://github.com/wasifnaeem/Test) and let me know where is the problem? – WasiF Dec 19 '18 at 18:57
  • @WasiF Try to use commonjs instead esnext. I cloned your repo, change esnext to commonjs and run `npm run server`, and it work correct. ) – Alex M. Dec 19 '18 at 20:43
  • @MrM Many thanks my friend, you saved my time. Changing to `commonjs` worked. :) – WasiF Dec 20 '18 at 04:57