【问题标题】:Webpack bundle ENOENT: no such file or directory fs.readdirSyncWebpack bundle ENOENT:没有这样的文件或目录 fs.readdirSync
【发布时间】:2017-04-08 03:02:19
【问题描述】:

我们想创建在 AWS Lambda 上运行的微型应用程序。我们正在为此研究 webpack 2。但是,我们有使用fs.readdirSync 获取文件/模块名称列表以生成模块列表的遗留代码。执行捆绑包时,我们收到错误 Error: ENOENT: no such file or directory, scandir '/innerLib',因为 webpack 不知道在文件 lib/lib.js 中执行 fs.readdirSync(path.resolve(__dirname, 'innerLib')); 并在捆绑期间解析文件名数组。

在不对遗留代码进行重大更改的情况下,我们可以对 wepback 采取哪些方法。我在下面和github上包含了一个简单的例子@

webpack.config.js

var path = require( 'path' );
var webpack = require( 'webpack' );

module.exports = {
  context: __dirname,
  entry: ['./index.js'],
  output: {
    filename: 'bundle.js',
  },
  target: 'node',
}

index.js

const  lib = require('./lib/lib.js');

lib.getModuleList((err, modules) => console.log(modules));

lib/lib.js

const fs = require('fs');
const path = require('path');
let moduleList = [];
let list = fs.readdirSync(path.resolve(__dirname, 'innerLib'));

exports.getModuleList = getModuleList;

function getModuleList(callback) {
  return callback(null, moduleList);
}

list.forEach(filename => {
  moduleList.push({
    name: filename
  });
});

lib/innerLib/a.js

console.log('a lib loaded');

lib/innerLib/b.js

console.log('b lib loaded');

【问题讨论】:

  • 你是如何执行包的?

标签: node.js webpack webpack-2


【解决方案1】:

您的问题是__dirname 正在解析为/。要让它与 webpack 一起工作,请设置:

node: {
  __dirname: true
}

在你的 webpack.config.js 中。添加之后,你的包对我来说执行得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-11
    • 2017-03-27
    • 2018-07-03
    • 2014-01-12
    • 1970-01-01
    • 2023-03-31
    • 2017-01-18
    • 2017-06-19
    相关资源
    最近更新 更多