Webpack bundle with 'jsonpath' dependency
Asked Answered
B

1

6

We use a webpack to bundle our lambdas before we deploy them. Ever since we added code that has a dependency on "jsonpath," the code fails immediately with the following.

TypeError: Cannot read property '1' of undefined at isFileType (fs.js:163:16) at Object.readFileSync (fs.js:346:16) at Object.28729 (/.../bundle.js:2:4710658)

I am pretty new to Webpack, so I am unsure where to go with this. Code works fine, but it's not bundled. Also, I have tried different versions of Webpack. Can someone suggest how to troubleshoot this?

Brittneybrittni answered 8/5, 2020 at 13:19 Comment(0)
H
0

I got a similar error within Azure Functions, which is an alternative to 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)

Since the error message was more expressive, it turned out, that the jsonpath package expects several files next to its location, since it uses require.resolve.

You can fix the issue by copying the required files next to your function, e.g. using copy-webpack-plugin in your 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'},
        ]
    })
],
Hoashis answered 3/12, 2021 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.