Webpack 和 TypeScript:无法解析 node.d.ts 中的module“child_process”

IT技术 node.js typescript reactjs webpack
2021-04-17 07:21:21

我试图通过awesome- typescript -loader使 webpack、typescript 和 react.js 协同工作,但我经常遇到错误。

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

这是我的 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 如下所示:

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

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

当我尝试通过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

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

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

3个回答

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

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

最终配置:

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 loader我知道有些人对节点的东西有问题。还有一些入门 套件可能会有所帮助。

免责声明:我是ts-loader.

尝试创建一个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 }
}