node.js - 如何修复 "@types/node/index.d.ts is not a module"?

标签 node.js typescript webpack webpack-2 typescript-typings

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

npm install --save-dev @types/node

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

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

import { Process } from 'node';

出现以下错误

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

我确定这里缺少一些非常基本的东西,但我无法弄清楚。

还有一些事情

  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"]
  }
}

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

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

import * as fs from "fs";

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

您可以了解更多here .

关于node.js - 如何修复 "@types/node/index.d.ts is not a module"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44416902/

相关文章:

Angular 7,预算警告,超出最大值,没有发现有什么问题?

node.js - 进行端到端测试时如何正确运行我的应用程序?

javascript - 设置 Angular js 应用程序 index.html 页面以仅包含单个 Controller

node.js - 无法获取店铺名称

typescript - 在 npm 项目中托管的环境 TypeScript 类型?

javascript - Webpack 和大型 vendor bundle

javascript - 在使用 gulp 构建的 Webpack 包中导出 Typescript 函数,浏览器中的空对象

javascript - 创建 heroku 服务器以记录统计信息时 Node cron 与 Node 计划

javascript - 如何修复 nz-table 删除 Angular 中的无数据显示

javascript - 设置动态内联样式 Angular 2 不起作用