我已经使用以下命令安装了 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.
我确定这里缺少一些非常基本的东西,但我无法弄清楚。
还有一些事情
- 我正在使用 Windows 8
- 我正在使用 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/