如何修复“@types/node/index.d.ts 不是模块"? [英] How to fix "@types/node/index.d.ts is not a module"?

查看:1151
本文介绍了如何修复“@types/node/index.d.ts 不是模块"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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';

import { Process } from 'node';

我收到以下错误

[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.

还有一些事情

  1. 我使用的是 Windows 8
  2. 我使用的是 Visual Studio Code

这是我的 tsconfig.json 的样子

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

这是我的 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'
  },
};

推荐答案

代替使用:

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

您需要更改您的tsconfig.json:

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

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

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

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

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

import * as fs from "fs";

您不需要从node"或@types/node"导入 fs.

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

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

You can learn more here.

这篇关于如何修复“@types/node/index.d.ts 不是模块"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆