简体   繁体   English

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

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

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我正在使用 Windows 8
  2. I am using Visual Studio Code我正在使用 Visual Studio 代码

Here is how my tsconfig.json looks这是我的tsconfig.json样子

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

And here is how my webpackconfig.js looks这是我的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'
  },
};

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 fs module.有时您需要从 Node.js 模块导入,例如fs模块。 You can do:你可以这样做:

import * as fs from "fs";

You don't need to import fs from "node" or from "@types/node".您不需要从“node”或“@types/node”导入fs

You can learn more here .您可以 在此处了解更多 信息

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 module or 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,则仅当您想“优化导入”时才需要在文件中添加导入

I just changed in tsconfig file from我刚刚在 tsconfig 文件中从

"typeRoots": ["./src/interfaces/index.d.ts"] 

this to这对

"typeRoots": ["node_modules/@types"]

and its working.及其工作。

After that I face issue with mongoose library.之后,我遇到了猫鼬库的问题。

Module 'mongoose' has no exported member 'QueryFindOneAndUpdateOptions'

Then I installed mongoose version library然后我安装了猫鼬版本库

npm install mongoose@5.8.1 

and it's working now.它现在正在工作。

In vscode在 vscode 中

Ctrl + Shift + p > Developer: Reload Window. Ctrl + Shift + p > 开发人员:重新加载 Window。

暂无
暂无

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

相关问题 如何修复Angular 2错误的node_modules/@types/systemjs/index.d.ts(333,13)? - How to fix node_modules/@types/systemjs/index.d.ts (333,13) of Angular 2 error? @ type / node的index.d.ts如何加载 - How index.d.ts of @type/node was loaded SPFx Webpart - node_modules/@types/ [prop types] 和 [react] index.d.ts:gulp 构建时加载“错误 TS1005” - SPFx Webpart - node_modules/@types/ [prop types] and [react] index.d.ts: loads of "error TS1005" on gulp build node_modules/@types/node/index.d.ts(20,1):错误 TS1084:无效的“引用”指令语法 - node_modules/@types/node/index.d.ts(20,1): error TS1084: Invalid 'reference' directive syntax npm发布index.d.ts不导出模块 - npm publishing index.d.ts not export module 在 index.d.ts npm 模块中包含自定义类型 - Include custom Typings in index.d.ts npm module TypeScript输入文件node.d.ts与index.d.ts - TypeScript typings file node.d.ts versus index.d.ts 目录/node_modules/@antv/g6-core/lib/types/index.d.ts(24,37) 中的 TypeScript 错误:预期类型。 TS1110 - TypeScript error in directory/node_modules/@antv/g6-core/lib/types/index.d.ts(24,37): Type expected. TS1110 从typescript模块自动生成index.d.ts,类型定义 - Auto generate index.d.ts, type definitions, from a typescript module Angular 7 node_modules/graphql-tag/lib/index.d.ts(2,57) 中的错误:错误 TS1005:“,”预期 - Angular 7 ERROR in node_modules/graphql-tag/lib/index.d.ts(2,57): error TS1005: ',' expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM