'How do I use @google-cloud/logging with Webpack, Babel, and Typescript?

I am using this example to get things started, and I am running into this error when I run my application:

Failed to compile.
Module parse failed: Unexpected token (39:35)
api/utils/logger.ts
File was processed with these loaders:
 * ../.yarn/__virtual__/@pmmmwh-react-refresh-webpack-plugin-virtual-216e5b16fb/0/cache/@pmmmwh-react-refresh-webpack-plugin-npm-0.5.1-c36df928dc-e6d6d35013.zip/node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
 * ../.yarn/__virtual__/source-map-loader-virtual-18915eaf0a/0/cache/source-map-loader-npm-3.0.0-2c53c811b5-c4459cef5a.zip/node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| }
| 
> async function quickstart(projectId: string, logName: string) {
|   // Creates a client
|   const logging = new Logging({ projectId });

I am assuming it is a loader I need to add to webpack.config.ts, but I am not sure what that would be. Having a hard time trying to research my error using @google-cloud/logging with Webpack or Typescript.

EDIT: I am using a monorepo (yarn workspace) for this project. The above error came in because I had my function using @google-cloud/logging in one workspace, and it was being called in another. I should be able to get that to work(?), but not sure. I just moved that function in to the workspace that is calling it, and the error was gone.

Now that I made that move, I am getting an error with Webpack5. A couple of sample errors:

➤ YN0000: ERROR in ../.yarn/cache/teeny-request-npm-7.1.3-d7a7a2dac2-81f27b1ebe.zip/node_modules/teeny-request/build/src/agents.js 23:14-28
➤ YN0000: Module not found: Error: Can't resolve 'url' in 'C:\Users\jwentling\Documents\GitHub\ui-mui\.yarn\cache\teeny-request-npm-7.1.3-d7a7a2dac2-81f27b1ebe.zip\node_modules\teeny-request\build\src'
➤ YN0000:
➤ YN0000: BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
➤ YN0000: This is no longer the case. Verify if you need this module and configure a polyfill for it.
➤ YN0000:
➤ YN0000: If you want to include a polyfill, you need to:
➤ YN0000:       - add a fallback 'resolve.fallback: { "url": require.resolve("url/") }'
➤ YN0000:       - install 'url'
➤ YN0000: If you don't want to include a polyfill, you can use an empty module like this:
➤ YN0000:       resolve.fallback: { "url": false }

➤ YN0000: ERROR in ../.yarn/cache/gtoken-npm-5.3.1-d316026d7b-7746679027.zip/node_modules/gtoken/build/src/index.js 10:11-24
➤ YN0000: Module not found: Error: Can't resolve 'fs' in '..\.yarn\cache\gtoken-npm-5.3.1-d316026d7b-7746679027.zip\node_modules\gtoken\build\src'

From what I understand, I need to make it so that Webpack5 includes polyfills for node.js core modules, but I am not sure how to do that with my limited knowledge on Webpack.

If more information is needed, let me know.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source