javascript - Webpack 无法呈现快速导入

标签 javascript node.js webpack

NodeJS/Express:在我添加之后,Webpack 无法将 src/server/app.js 转译为 dist/server/app.js import express from 'express' 到 src 文件 (src/server/app.js)。


错误信息如下:

ERROR in ./~/express/lib/request.js

详细说明:

Module not found: Error: Can't resolve 'net'
 in '/Users/timo/Desktop/Eggs/node_modules/express/lib'

ERROR in ./~/express/lib/view.js

详细说明:

Module not found: Error: Can't resolve 'fs'
 in '/Users/timo/Desktop/Eggs/node_modules/express/lib'

我是否必须更改我的 webpack.config.babel.js 文件中的某些内容?

这里是:

export default [
  {
    entry: './src/server/app.js',
    output: {
      path: './dist/server',
      filename: 'app.js'
    },
    module: {
      loaders: [{
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: { presets: ['es2015', 'react'] }
      }]
    }
  },
    {
    entry: './src/client/app.js',
    output: {
      path: './dist/client',
      filename: 'app.js'
    },
    module: {
      loaders: [{
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: { presets: ['es2015', 'react'] }
      }]
    }
  }
]

最佳答案

尝试设置 "node" target对于您的服务器端代码:

{
  entry  : './src/server/app.js',
  target : 'node',
  ...
}

默认情况下,Webpack 会编译以供在网络浏览器中使用,它不支持许多只对在服务器端运行有意义的模块(如 netfs)。

关于javascript - Webpack 无法呈现快速导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42558353/

相关文章:

javascript - 单页多个进度条

node.js - 使用 Node.JS HTTP 服务器的工作流程

node.js - Meteor bundle 只是 "LISTENING",本地主机上无法访问应用程序 :3000

webpack - serverless-webpack 找不到模块 './node/NodeTemplatePlugin'

javascript - Webpack JS 文件中出现意外标记

javascript - 如何使用 webpack 包含 worker/process 文件

javascript - 如何使用 Angular 改变体型

javascript - 在 CLI 构建中使用 CDN 组件?

javascript - 表标记内的 jquery 复选框无法访问

node.js - 验证 Node js 中的特定请求