Webpack和TypeScript:无法parsingnode.d.ts中的模块“child_process”

我试图让webpack,typescript和react.js通过awesome- typescript -loader一起工作,但是我经常得到错误。

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

这是我的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 dev服务器时,我的index.ts如下所示:

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

同样,当我把参考path。

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

 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文件,仍然没有运气。

有没有人能够顺利地完成这个组合工作? 或者我应该只使用一个不同的网页包装器? 我真的想要得到它与webpack的工作。

你所缺less的是一个关键的target: 'node'

这可以确保您所针对的环境是Node.js,而不是浏览器,因此会忽略本地依赖项。

最终configuration:

 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加载器 。 我知道一些有问题的节点的东西。 也有一些启动 工具包 ,可以帮助。

免责声明:我是ts-loader的维护者。

尝试创build一个tsconfig.json文件。

例如,你正在使用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 } }