webpack不支持require.extensions。请改用装载机

时间:2017-07-23 06:40:56

标签: node.js typescript express webpack ejs

我使用express + webpack3 + ejs + typescript

当我构建时,stdout输出会给我一些警告:

WARNING in ./node_modules/ejs/lib/ejs.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./src/environment.ts 4:10-24
 @ ./src/server.ts

WARNING in ./node_modules/ejs/lib/ejs.js
require.extensions is not supported by webpack. Use a loader instead.
 @ ./src/environment.ts 4:10-24
 @ ./src/server.ts

WARNING in ./node_modules/express/lib/view.js
80:29-41 Critical dependency: the request of a dependency is an expression

这是webpack.config.ts的一部分:

import * as webpack from 'webpack';
import * as path from 'path';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';

const config: webpack.Configuration = {
  target: 'node',
  devtool: 'source-map',
  entry: {
    server: path.resolve(__dirname, './src/server.ts')
  },
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'server.js'
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        loader: 'ts-loader'
      }
    ]
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js']
  },
  plugins: [
    new CopyWebpackPlugin([
      { from: './src/views', to: 'views' },
      { from: './src/public', to: 'public' }
    ])
  ]
};

export default config;

tsconfig.json的一部分:

"include": [
    "src/server.ts",
    "src/typings"
  ],
  "exclude": [
    "node_modules",
    "src/public",
    "src/views"
  ]

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我通过将以下代码添加到我的webpack.config.js文件中来解决了这个问题:

resolve: {
   alias: {
       'express-handlebars': 'handlebars/dist/handlebars.js'
   }
}

答案 1 :(得分:1)

最好的解决方案是使用 webpack-node-externals 在使用 webpack 作为后端时完全不用担心 node_modules。

<块引用>

当为后端捆绑 Webpack 时 - 您通常不想捆绑其 node_modules 依赖项。这个库创建了一个外部函数,在 Webpack 中捆绑时忽略 node_modules。

答案 2 :(得分:0)

此设置使用ejs,特定于ejs的解决方案是在您的webpack配置中进行设置:

resolve: {
   alias: {
     'ejs': 'ejs.min.js'
   }
}