nodejs 需要在 TypeScript 文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12742082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:25:27  来源:igfitidea点击:

nodejs require inside TypeScript file

node.jstypescript

提问by Zdenek Sejcek

How do I load a regular NodeJS module (from node_modules) from within a TypeScriptclass?

如何node_modulesTypeScript类中加载常规 NodeJS 模块(来自)?

When I try to compile .tsfile that contains:

当我尝试编译.ts包含以下内容的文件时:

var sampleModule = require('modulename');

Compiler prompts that I can't use require in this scope. (that line is at the beginning of the file).

编译器提示我不能在这个范围内使用require。(该行位于文件的开头)。

回答by Valentin

Typescript will always complain when it is unable to find a symbol. The compiler comes together with a set of default definitions for window, documentand such specified in a file called lib.d.ts. If I do a grep for requirein this file I can find no definition of a function require. Hence, we have to tell the compiler ourselves that this function will exist at runtime using the declaresyntax:

当找不到符号时,打字稿总是会抱怨。编译器附带了一组 的默认定义windowdocument并在名为lib.d.ts. 如果我require在这个文件中做一个 grep ,我找不到函数的定义require。因此,我们必须自己告诉编译器该函数将在运行时使用以下declare语法存在:

declare function require(name:string);
var sampleModule = require('modulename');

On my system, this compiles just fine.

在我的系统上,这编译得很好。

回答by Jesse

The correct syntax is:

正确的语法是:

import sampleModule = require('modulename');

or

或者

import * as sampleModule from 'modulename';

Then compile your TypeScript with --module commonjs.

然后使用--module commonjs.

If the package doesn't come with an index.d.tsfile and its package.jsondoesn't have a "typings"property, tscwill bark that it doesn't know what 'modulename'refers to. For this purpose you need to find a .d.tsfile for it on http://definitelytyped.org/, or write one yourself.

如果包没有附带index.d.ts文件并且它package.json没有"typings"属性,tsc则会吠叫它不知道'modulename'指的是什么。为此,您需要.d.tshttp://definelytyped.org/上为它找到一个文件,或者自己编写一个。

If you are writing code for Node.js you will also want the node.d.tsfile from http://definitelytyped.org/.

如果您正在为 Node.js 编写代码,您还需要node.d.ts来自http://definelytyped.org/的文件。

回答by rharriso

The best solution is to get a copy of Node's type definitions. This will solve all kinds of dependency issues, not only require(). This was previously done using packages like typings, but as Mike Chamberlain mentioned, Typings are deprecated. The modern way is doing it like this:

最好的解决方案是获取 Node 类型定义的副本。这将解决各种依赖问题,不仅是require(). 这以前是使用像 那样的包完成的typings,但正如 Mike Chamberlain 所提到的,打字已被弃用。现代方式是这样做的:

npm install --save-dev @types/node

Not only will it fix the compiler error, it will also add the definitions of the Node API to your IDE.

它不仅会修复编译器错误,还会将 Node API 的定义添加到您的 IDE。

回答by Oded Breiner

Use typings to access node functions from TypeScript:

使用类型从 TypeScript 访问节点函数:

typings install env~node --global

If you don't have typings install it:

如果你没有打字安装它:

npm install typings --global