首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用本机'fs‘库引发WSL错误的NodeJS+Webpack+Docker项目

使用本机'fs‘库引发WSL错误的NodeJS+Webpack+Docker项目
EN

Stack Overflow用户
提问于 2019-10-22 16:04:44
回答 2查看 1.1K关注 0票数 2

我有一个NodeJS项目,我正在建设与Webpack,并运行在一个码头集装箱。这是在Linux环境中开发的,但我决定将其移到WSL (Windows子系统用于Linux),因为这将使开发团队的工作变得更容易。然而,要让它在WSL上运行是很困难的。

目前,该项目建设没有任何问题,码头似乎也运行顺利。但是,当我在浏览器上打开项目时,没有加载任何内容。控制台上有以下错误消息:

代码语言:javascript
复制
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时,我看到以下几行:

代码语言:javascript
复制
// 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版本如下:

代码语言:javascript
复制
me@computer:.../addin$ nodejs --version
v12.11.1
me@computer:.../addin$ npm --version
6.12.0

windows上的NodeJS是:

代码语言:javascript
复制
PS H:\> node --version
v10.15.3

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

webpack.server.config.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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' ]
    })
  ]
}

而构建命令是:

代码语言:javascript
复制
rm -rf dist && webpack --mode development --display-error-details --config webpack.server.config.js && webpack --mode development

我想不出怎么解决这个问题了。我已经尝试删除和重新安装nodejs,删除所有码头映像等。任何建议将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-28 13:49:30

修正了通过安装这套npm方案,在server.js上导入它,和猴子扳手:

代码语言:javascript
复制
var rp = require('fs.realpath')
rp.monkeypatch()

不幸的是,修复它让我对WSL和节点的问题有了更深入的了解,但至少它是有效的。

编辑:

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

我最初发布的webpack.config.js用于expressJS服务器,而代码的另一部分则用于应用程序的前端。

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

我希望这能帮助那些遇到这个问题的人。

票数 1
EN

Stack Overflow用户

发布于 2019-10-22 16:25:02

你的package.json里有package.json吗?我记得当我忘记将jquery添加到index.html时,创建-React App中出现了这个错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58508257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档