简体   繁体   English

导入语句在打字稿中不起作用

[英]Import statement does not work in typescript

I read about import and export declarations in javascript.我阅读了 javascript 中的导入和导出声明。 Then I'm trying to import a class in a file using 'import' keyword.然后我尝试使用'import'关键字在文件中导入一个类。 But despite having read about the declaration of modules in node.js y get a error when execute node.但是,尽管已经阅读了 node.js 中模块的声明,但在执行 node 时会出错。

My index.ts :我的 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我的课程/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我的 package.json

在此处输入图像描述

But when i try to run npm run start ...但是当我尝试运行npm run start ...

在此处输入图像描述

How is your tsconfig.json ?你的 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我假设您缺少一些配置,这些配置可以将 typescript 文件编译为节点可以运行的兼容 javascript,这是应该工作的 tsconfig.json 示例

{
  "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所以基本上你需要有moduleResolutionnode 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.也不要忘记节点不运行 ts 文件,你需要先用 tsc 编译。 Or if you prefer you can always run npx ts-node index.ts或者,如果您愿意,您可以随时运行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 . ts-node 它的作用是首先将您的代码从 ts 编译为 js 并使用生成的 js 运行它节点,您可以查看有关 ts 节点的更多信息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 .如果你想避免使用 ts-node ,你总是可以先使用npx tsc编译,它是 typescript 的编译器,一旦你有了 javascript,你可以运行node index.js或简单地node . . . Note that you have to check where you're output folder for the js file using the outDir on the ts configuration, if outDir is configuration is missing will add js files in the same place the ts exists https://www.typescriptlang.org/docs/handbook/compiler-options.html请注意,您必须使用 ts 配置上的outDir检查 js 文件的输出文件夹的位置,如果缺少outDir配置,则会在 ts 存在的同一位置添加 js 文件https://www.typescriptlang.org /docs/handbook/compiler-options.html

If you want to use import/export syntax, you have following options:如果您想使用import/export语法,您有以下选项:

  • use node's native experimental ES modules使用 node 原生的实验性 ES 模块
  • Use esm npm package使用esm npm 包
  • Transpile to commonjs using babel or similar tooling.使用 babel 或类似工具转换为 commonjs。

Using node's native ES modules:使用节点的原生 ES 模块:

Add this to your package.json:将此添加到您的 package.json 中:

{
  "type": "module"
}

And change your start script to: "start": "node index.js" (assumes you're using node > v13.3.0).并将您的启动脚本更改为: "start": "node index.js" (假设您使用的是 node > v13.3.0)。 If you're using node v12 or above, you can try: node --experimental-modules index.js如果您使用的是 node v12 或更高版本,您可以尝试: node --experimental-modules index.js

Use esm package:使用esm包:

Docs here: https://github.com/standard-things/esm此处的文档: https ://github.com/standard-things/esm


You need to compile your .ts files to .js files first though.不过,您需要先将.ts文件编译为.js文件。 See TypeScript docs for how to do that.请参阅TypeScript 文档了解如何执行此操作。

As far as i know, you can't use the import statement in node js yet.据我所知,你还不能在 node js 中使用 import 语句。 But you could use the experimental features(.mjs files).但是您可以使用实验功能(.mjs 文件)。

This article explains the module/file imports in detail. 本文详细解释了模块/文件的导入。

i'm using ts-node and this works for me我正在使用 ts-node 这对我有用

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

even if your file's name is file.ts using file.js works即使您的文件名是 file.ts 使用 file.js 也可以

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM