【问题标题】:NodeJS+Webpack+Docker project on WSL throwing errors with native 'fs' libraryWSL 上的 NodeJS+Webpack+Docker 项目使用本机“fs”库引发错误
【发布时间】:2020-02-18 20:08:39
【问题描述】:

我有一个使用 Webpack 构建并在 Docker 容器中运行的 NodeJS 项目。这是在 Linux 环境中开发的,但我决定尝试将其移至 WSL(Linux 的 Windows 子系统),因为这会使开发团队的事情变得更容易。不过,让它在 WSL 上运行一直很困难。

目前项目构建没有问题,Docker 似乎也运行顺利。但是,当我在浏览器上打开项目时,什么都没有加载。控制台显示以下错误消息:

Uncaught TypeError: Cannot read property 'native' of undefined
    at Object../node_modules/fs-extra/lib/fs/index.js (index.js:107)
    at __webpack_require__ (bootstrap:19)
    at Object../node_modules/fs-extra/lib/index.js (index.js:6)
    at __webpack_require__ (bootstrap:19)
    at Object.<anonymous> (RollingFileWriteStream.js:2)
    at Object../node_modules/streamroller/lib/RollingFileWriteStream.js (RollingFileWriteStream.js:265)
    at __webpack_require__ (bootstrap:19)
    at Object../node_modules/streamroller/lib/index.js (index.js:2)
    at __webpack_require__ (bootstrap:19)
    at Object.<anonymous> (file.js:3)

当我检查index.js:107 时,我看到以下几行:

// fs.realpath.native only available in Node v9.2+
if (typeof fs.realpath.native === 'function') {
  exports.realpath.native = u(fs.realpath.native)
}

但是,我运行的所有节点版本都是 10+。我的基础镜像是node:12(更具体地说,版本 12.13.0)。 WSL 上的 Nodejs 和 npm 版本是:

me@computer:.../addin$ nodejs --version
v12.11.1
me@computer:.../addin$ npm --version
6.12.0

Windows 上的 NodeJS 是:

PS H:\> node --version
v10.15.3

我不确定这是否相关,但这是我的 webpack 配置文件:

webpack.server.config.js:

const path = require('path')
const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals')
module.exports = {
  entry: {
    server: './src/server/server.js',
  },
  output: {
    path: path.join(__dirname, 'dist'),
    publicPath: '/',
    filename: '[name].js'
  },
  target: 'node',
  node: {
    // Need this when working with express, otherwise the build fails
    __dirname: false,   // if you don't put this is, __dirname
    __filename: false,  // and __filename return blank or /
    fs: 'empty'
  },
  externals: [nodeExternals()], // Need this to avoid error when working with Express
  module: {
    rules: [
      {
        // Transpiles ES6-8 into ES5
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  }
}

webpack.config.js:

const path = require("path")
const webpack = require('webpack')
const HtmlWebPackPlugin = require("html-webpack-plugin")

module.exports = {
  entry: {
    main: './src/js/index.tsx'
  },
  output: {
    path: path.join(__dirname, 'dist'),
    publicPath: '/',
    filename: '[name].js'
  },
  target: 'web',
  devtool: 'source-map',
  resolve: {
    extensions: ['.ts', '.tsx', '.html', '.js']
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: 'ts-loader'
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader",
      },
      {
        // Loads the javacript into html template provided.
        // Entry point is set below in HtmlWebPackPlugin in Plugins
        test: /\.html$/,
        use: [
          {
            loader: "html-loader",
            //options: { minimize: true }
          }
        ]
      },
      {
        test: /\.css$/,
        use: [ 'style-loader', 'css-loader' ]
      },
      {
       test: /\.(png|svg|jpg|gif)$/,
       use: ['file-loader']
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/html/index.html",
      filename: "./index.html",
      excludeChunks: [ 'server' ]
    })
  ]
}

而构建命令是:

rm -rf dist && webpack --mode development --display-error-details --config webpack.server.config.js && webpack --mode development

我不知道如何解决这个问题。我已尝试删除并重新安装 nodejs,删除所有 docker 映像等。任何建议将不胜感激。

【问题讨论】:

  • 检查 docker 容器内的节点版本:docker exec -it yourcontainerid bash
  • 嗨@CarlosRafaelRamirez。容器内节点的版本为v12.13.0

标签: node.js docker webpack windows-subsystem-for-linux


【解决方案1】:

通过安装this npm package,在server.js 上导入它,以及猴子扳手修复:

var rp = require('fs.realpath')
rp.monkeypatch()

不幸的是,修复它并没有让我更深入地了解 WSL 和节点的问题,但至少它可以工作。

编辑:

由于这个问题似乎与某些人有关,我发现 真正的 问题是我试图在与 target: 'web' 捆绑的类中使用 fs(我发布的第二个配置文件)。这是代码的另一部分,我没有想到这可能是问题所在。

我最初发布的webpack.config.js 是针对expressJS 服务器的,而这部分代码是针对应用程序的前端的。

据我了解,target: 'web' 告诉 Webpack 不要捆绑和 NodeJS 函数,因为此代码将在浏览器中运行。 target: 'node' 适用于将在节点环境中运行的代码(即,将在后端运行的 expressJS 服务器)

我希望这对遇到此问题的人有所帮助。

【讨论】:

    【解决方案2】:

    你的 package.json 中有 jquery 吗?我记得当我忘记将 jquery cdn 添加到 index.html 时,使用 Create-React-App 遇到了这个错误

    【讨论】:

    • 感谢您的建议。我没有cdn,但没有解决问题。
    • 对,因为 webpack。你已经npm install jquery了吗?
    • 我确实运行了npm install jquery。问题依然存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 2022-01-13
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2021-09-12
    相关资源
    最近更新 更多