typescript 如何修复“@types/node/index.d.ts 不是模块”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44416902/
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-10-21 04:36:49  来源:igfitidea点击:

How to fix "@types/node/index.d.ts is not a module"?

node.jstypescriptwebpackwebpack-2typescript-typings

提问by Suhas

I have installed node type definitions using the following command

我已经使用以下命令安装了节点类型定义

npm install --save-dev @types/node

After that, when I try to import the node type definitions using the following statement

之后,当我尝试使用以下语句导入节点类型定义时

import { Process } from '@types/node';

or

或者

import { Process } from 'node';

I get the following error

我收到以下错误

[ts] File '<root_path>/node_modules/@types/node/index.d.ts' is not a module.

I am sure there is something very basic that I am missing here but I cannot figure out.

我确信我在这里遗漏了一些非常基本的东西,但我无法弄清楚。

Few more things

还有几件事

  1. I am using Windows 8
  2. I am using Visual Studio Code
  1. 我使用的是 Windows 8
  2. 我正在使用 Visual Studio 代码

Here is how my tsconfig.jsonlooks

这是我的tsconfig.json样子

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "outDir": "lib",
    "typeRoots": [
        "node_modules/@typings"
    ]
  },
  "exclude": [
    "node_modules"
  ]
}

And here is how my webpackconfig.jslooks

这是我的webpackconfig.js样子

var path = require('path');

module.exports = {  
  entry: './ts/handler.ts',
  target: 'node',
  module: {
    loaders: [
      { test: /\.ts(x?)$/, loader: 'ts-loader' },      
      { test: /\.json$/, loader: 'json-loader' }
    ]
  },
  resolve: {
    extensions: ['.ts', '.js', '.tsx', '.jsx']
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: 'handler.js'
  },
};

采纳答案by Remo H. Jansen

Instead of using:

而不是使用:

import { Process } from '@types/node';

You need to change your tsconfig.json:

你需要改变你的tsconfig.json

{
  "compilerOptions": {
    ...
    "typeRoots": ["node_modules/@types"]
  }
}

After doing that the process variable becomes available as a global.

这样做之后,流程变量就可以作为全局变量使用了。

Sometimes you will need to import from a Node.js module like for example the fsmodule. You can do:

有时您需要从 Node.js 模块导入,例如fs模块。你可以做:

import * as fs from "fs";

You don't need to import fsfrom "node" or from "@types/node".

您不需要fs从“节点”或“@types/node”导入。

You can learn more here.

您可以在此处了解更多信息

回答by OrcusZ

It's because inside the node typing file any module has declared with name node.

这是因为在节点类型文件中,任何模块都使用名称node 进行了声明。

If you use

如果你使用

import { Process } from 'node';

TypeScript will try too find a node moduleor node namespace

TypeScript 也会尝试找到节点模块节点命名空间

Here you can load the complete file using

在这里您可以使用加载完整的文件

import 'node';

In your case you want to get only Process from NodeJS namespace :

在您的情况下,您只想从 NodeJS 命名空间获取 Process :

import 'NodeJS';

Aftert that you just need to call it like this :

之后你只需要像这样调用它:

class toto implements NodeJS.Process{

}

EDIT :

编辑 :

If you use TypeScript >= 2.0 you should not need to add import in your file, only if you want to "optimize import"

如果您使用 TypeScript >= 2.0,则仅当您想“优化导入”时才需要在文件中添加导入