Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

node.js - 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
  2. I am using Visual Studio Code

Here is how my tsconfig.json looks

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

And here is how my webpackconfig.js looks

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'
  },
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of using:

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

You need to change your 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. You can do:

import * as fs from "fs";

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

You can learn more here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...