Webpack 和 TypeScript:无法解析 node.d.ts 中的模块“child_process" [英] Webpack and TypeScript: Cannot resolve module 'child_process' in node.d.ts

查看:135
本文介绍了Webpack 和 TypeScript:无法解析 node.d.ts 中的模块“child_process"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 awesome-typescript- 让 webpack、typescript 和 react.js 一起工作loader,但我经常出错.

I tried to get webpack, typescript and react.js working together via awesome-typescript-loader, but I constantly get errors.

我在 0.3.0-rc.2 和 webpack 1.8.9 版本中使用了很棒的打字稿加载器

I am using awesome typescript loader at Version 0.3.0-rc.2 and webpack 1.8.9

这是我的 webpack.config.js:

This is my webpack.config.js:

module.exports = {
    entry: './ui/index.ts',
    output: {
        path: __dirname + '/build-ui',
        filename: 'app.js',
        publicPath: 'http://localhost:8090/assets'
    },
    module: {
        loaders: [
            {
                test: /\.jsx$/,
                loader: 'jsx-loader?insertPragma=React.DOM&harmony'
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            },
            {
                test: /\.scss$/,
                loader: "style-loader!css-loader!sass-loader"
            },
            {
                test: /\.(png|jpg)$/,
                loader: 'url-loader?limit=8192'
            },
            {
                test: /\.ts$/,
                loader: 'awesome-typescript-loader'
            }
        ]
    },
    resolve: {
        extensions: ['', '.js', '.jsx', '.ts']
    }
};

当我运行 webpack 开发服务器时,我的 index.ts 看起来像这样:

When I run the webpack dev server and my index.ts looks like this:

alert('hello');

它说明了以下错误:

ERROR in ./ui/index.ts
/Users/..../typings/node/node.d.ts:29:12 
Subsequent variable declarations must have the same type.  Variable 'require' must be of type 'WebpackRequire', but here has type '{ (id: string): any; resolve(id: string): string; cache: any; extensions: any; main: any; }'.

当我放入参考路径时也是如此.

Same when I put in the reference path.

当我尝试通过 import React = require('react'); 导入 React.js 时,它指出:

When I try to import the React.js via import React = require('react'); it states:

ERROR in ./ui/index.ts
Module build failed: Cannot resolve module 'child_process' in /..../typings/node
    Required in /..../typings/node/node.d.ts

我从加载器存储库中复制了 node.d.ts 文件,但仍然没有运气.

I copied the node.d.ts file from the loader repo, still no luck.

有没有人能够顺利地让这个组合工作?还是我应该使用不同的 Web 打包程序?我真的很想让它与 webpack 一起使用.

Has anybody been able to get this combination work smoothly? Or should I just use a different web packager? I'd really would like to get it to work with webpack.

推荐答案

你所缺少的只是一个键 target: 'node'.

All you're missing is a key target: 'node'.

这可确保您的目标环境是 Node.js 而不是浏览器,因此将忽略本机依赖项.

This makes sure that the environment you are targeting is Node.js and not the browser, and will therefore ignore native dependencies.

最终配置:

module.exports = {
    entry: './ui/index.ts',
    target: 'node',
    output: {
        path: __dirname + '/build-ui',
        filename: 'app.js',
        publicPath: 'http://localhost:8090/assets'
    },
    module: {
        loaders: [
            {
                test: /\.jsx$/,
                loader: 'jsx-loader?insertPragma=React.DOM&harmony'
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            },
            {
                test: /\.scss$/,
                loader: "style-loader!css-loader!sass-loader"
            },
            {
                test: /\.(png|jpg)$/,
                loader: 'url-loader?limit=8192'
            },
            {
                test: /\.ts$/,
                loader: 'awesome-typescript-loader'
            }
        ]
    },
    resolve: {
        extensions: ['', '.js', '.jsx', '.ts']
    }
};

这篇关于Webpack 和 TypeScript:无法解析 node.d.ts 中的模块“child_process"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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