Monic
Monic

Reputation: 123

Import statement does not work in typescript

I read about import and export declarations in javascript. Then I'm trying to import a class in a file using 'import' keyword. But despite having read about the declaration of modules in node.js y get a error when execute node.

My index.ts :

import Server from "./classes/server";
import router from './routes/router';

const server = new Server();

server.app.use('/', router);

server.start(() => {
    console.log("server starting!");
})

My classes/server.ts

import { SERVER_PORT } from './../global/enviroment';
import express from 'express';

class Server {
    public app: express.Application;
    public port: number;

    constructor(){
        this.app = express();
        this.port = SERVER_PORT;
    }

    start(callback: any){
        this.app.listen(this.port, callback);
    }
}
export default Server;

My package.json

enter image description here

But when i try to run npm run start...

enter image description here

Upvotes: 11

Views: 26943

Answers (4)

Rajodiya Jeel
Rajodiya Jeel

Reputation: 347

i'm using ts-node and this works for me

import {ClassName} from './path/to/file.js/'  //not file.ts

even if your file's name is file.ts using file.js works

Upvotes: 0

dege
dege

Reputation: 2954

How is your tsconfig.json ?

I assume you are missing some configuration that that enables the compilation of the typescript file into a compatible javascript that node can run, here's an example of tsconfig.json that should work

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "preserveConstEnums": true,
    "strict": true,
    "target": "es2017",
}

So essentially you will need to have moduleResolution to node https://www.typescriptlang.org/docs/handbook/module-resolution.html

Also don't forget that node doesn't run ts files, you need to compile then first with tsc. Or if you prefer you can always run npx ts-node index.ts

EDIT:

ts-node what it does is compile your code first from ts to js and run it node with the generated js, you can check more information about ts node https://github.com/TypeStrong/ts-node.

If you want to avoid to use ts-node your can always compile first using npx tsc which is the compiler from typescript, once you have the javascript you can run node index.js or simply node .. Note that you have to check where you're output folder for the js file using the outDir on the ts configuration, if outDiris configuration is missing will add js files in the same place the ts exists https://www.typescriptlang.org/docs/handbook/compiler-options.html

Upvotes: 9

Sid Vishnoi
Sid Vishnoi

Reputation: 1318

If you want to use import/export syntax, you have following options:

  • use node's native experimental ES modules
  • Use esm npm package
  • Transpile to commonjs using babel or similar tooling.

Using node's native ES modules:

Add this to your package.json:

{
  "type": "module"
}

And change your start script to: "start": "node index.js" (assumes you're using node > v13.3.0). If you're using node v12 or above, you can try: node --experimental-modules index.js

Use esm package:

Docs here: https://github.com/standard-things/esm


You need to compile your .ts files to .js files first though. See TypeScript docs for how to do that.

Upvotes: 2

Mensur Grišević
Mensur Grišević

Reputation: 593

As far as i know, you can't use the import statement in node js yet. But you could use the experimental features(.mjs files).

This article explains the module/file imports in detail.

Upvotes: 0

Related Questions