简体   繁体   English

Webpack和TypeScript:无法解析node.d.ts中的模块'child_process'

[英]Webpack and TypeScript: Cannot resolve module 'child_process' in node.d.ts

I tried to get webpack, typescript and react.js working together via awesome-typescript-loader , but I constantly get errors. 我试图通过awesome- typescript -loader让webpack,typescript和react.js一起工作,但我经常遇到错误。

I am using awesome typescript loader at Version 0.3.0-rc.2 and webpack 1.8.9 我在版本0.3.0-rc.2和webpack 1.8.9上使用了很棒的打字稿加载程序

This is my webpack.config.js: 这是我的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']
    }
};

When I run the webpack dev server and my index.ts looks like this: 当我运行webpack dev服务器时,我的index.ts看起来像这样:

alert('hello');

It states the following error: 它声明了以下错误:

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. 当我放入参考路径时也一样。

When I try to import the React.js via import React = require('react'); 当我尝试通过import React = require('react');导入React.js时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

I copied the node.d.ts file from the loader repo, still no luck. 我从loader repo复制了node.d.ts文件,但仍然没有运气。

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. 我真的很想让它与webpack一起工作。

All you're missing is a key target: 'node' . 你所缺少的只是一个关键target: 'node'

This makes sure that the environment you are targeting is Node.js and not the browser, and will therefore ignore native dependencies. 这可以确保您定位的环境是Node.js而不是浏览器,因此将忽略本机依赖项。

Final Config: 最终配置:

 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'] } }; 

You could consider using a different webpack TypeScript loader . 您可以考虑使用不同的webpack TypeScript加载程序 I know a few had issues with node stuff. 我知道有一些节点的问题。 There are also a couple starter kits that may help out. 还有一些可能有用的入门 套件

Disclaimer: I'm the maintainer of ts-loader . 免责声明:我是ts-loader的维护者。

Try creating a tsconfig.json file. 尝试创建tsconfig.json文件。

For example, as you are using awesome-typescript-loader this should work: 例如,当您使用awesome-typescript-loader这应该工作:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "noEmitHelpers": true
  },
  "exclude": [
    "node_modules"
  ],
  "awesomeTypescriptLoaderOptions": {
    "forkChecker": true
  },
  "compileOnSave": false,
  "buildOnSave": false,
  "atom": { "rewriteTsconfig": false }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM