[英]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还有一些事情
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
。
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.