最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Import statement does not work in typescript - Stack Overflow

programmeradmin3浏览0评论

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

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

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

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

Share Improve this question edited Dec 5, 2019 at 14:24 sonan 1113 silver badges13 bronze badges asked Dec 5, 2019 at 13:53 MonicMonic 1231 gold badge1 silver badge7 bronze badges 1
  • when execute npm run start run the command node . (i have added the package.json picture to the issue) – Monic Commented Dec 5, 2019 at 14:05
Add a comment  | 

4 Answers 4

Reset to default 9

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

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.

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.

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

发布评论

评论列表(0)

  1. 暂无评论