具有“jsonpath”依赖项的 Webpack 捆绑包

Kic*_*Mhl 6 bundle jsonpath webpack

在部署 lambda 之前,我们使用 webpack 来捆绑它们。自从我们添加了依赖于“jsonpath”的代码以来,代码立即失败,并显示以下内容。

TypeError:无法在 Object.28729 (/.../bundle.js:2) 的 Object.readFileSync (fs.js:346:16) 的 isFileType (fs.js:163:16) 处读取未定义的属性“1”: 4710658)

我对 Webpack 还很陌生,所以我不确定该去哪里。代码工作正常,但它没有捆绑。另外,我尝试过不同版本的 Webpack。有人可以建议如何解决这个问题吗?

ssc*_*eck 0

我在Azure Functions中遇到了类似的错误,它是AWS Lambdas的替代方案。

Error: ENOENT: no such file or directory, open './node_modules/jsonpath/include/module.js'
    at Object.openSync (fs.js:476:3)
    at Object.readFileSync (fs.js:377:35)
Run Code Online (Sandbox Code Playgroud)

由于错误消息更具表现力,事实证明,该jsonpath包需要在其位置旁边有多个文件,因为它使用require.resolve.

您可以通过复制函数旁边所需的文件来解决问题,例如copy-webpack-plugin在您的webpack.config.js.

plugins: [
    new CopyWebpackPlugin({
        patterns: [
            // required node_modules for jsonpath (see https://github.com/dchester/jsonpath/search?q=require.resolve)
            {from: 'node_modules/jsonpath/include', to: 'node_modules/jsonpath/include'},
            {from: 'node_modules/esprima', to: 'node_modules/esprima'},
        ]
    })
],
Run Code Online (Sandbox Code Playgroud)