Webpack 和 TypeScript:无法解析 node.d.ts 中的模块“child_process”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30000827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webpack and TypeScript: Cannot resolve module 'child_process' in node.d.ts
提问by Johannes Klau?
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 开发服务器时,我的 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');
it states:
当我尝试通过import React = require('react');
它导入 React.js时:
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.
我从加载程序存储库中复制了 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.
有没有人能够顺利地使这种组合工作?或者我应该使用不同的 Web 打包程序?我真的很想让它与 webpack 一起使用。
采纳答案by AP.
All you're missing is a key target: 'node'
.
你所缺少的只是一个 key 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']
}
};
回答by James Brantly
You could consider using a different webpack TypeScript loader. I know a few had issues with node stuff. There are also a couple starterkitsthat may help out.
您可以考虑使用不同的webpack TypeScript loader。我知道有些人对节点的东西有问题。还有一些入门套件可能会有所帮助。
Disclaimer: I'm the maintainer of ts-loader
.
免责声明:我是ts-loader
.
回答by acdcjunior
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 }
}