openapi-client-axios icon indicating copy to clipboard operation
openapi-client-axios copied to clipboard

error with dependencies // jsdevtools

Open binarykiwi opened this issue 4 years ago • 10 comments

Hi all, I've got a system up an running using Vue.js 3.0.5 and Vite as a build system. As I need to use a Swagger API in my project I thought it might be a good deal using this openapi-client-axios. Actually I get an error as soon as I import the client:

import OpenAPIClientAxios from 'openapi-client-axios'
const api = new OpenAPIClientAxios({ definition: 'api.json' })
api.init()

These lines occur the error message:

[vite] new dependencies found: openapi-client-axios, updating...
 > node_modules/@jsdevtools/ono/esm/types.js: error: No matching export for import "inspect"
    1 │ import { inspect } from "util";
      ╵          ~~~~~~~

[vite] error while updating dependencies:
Error: Build failed with 1 error:
node_modules/@jsdevtools/ono/esm/types.js:1:9: error: No matching export for import "inspect"

Is it possible that some of the dependencies or this entire project are not ready for Vue 3 or what am I doing wrong? Thanks a lot.

binarykiwi avatar Feb 19 '21 11:02 binarykiwi

Looks like you are missing a polyfill for the node.js util core package.

Try npm install --save util

anttiviljami avatar Feb 19 '21 17:02 anttiviljami

Thanks so much for the response 👍 It seams this fixes the error explained above. I don't know why my node (15.8.0) didn't have this module. Normally node does this by default I thought.

One step further. Now it ends in the browser console:

Uncaught ReferenceError: process is not defined
    at util.js:109

Do you have a good solution for this problem as well?

binarykiwi avatar Feb 19 '21 18:02 binarykiwi

Sorry, haven't dug that deep into vue but for whatever reason it seems like util is not being included by vue. Installing the util package doesn't work because process is not defined in the browser.

You might be able to work around that error by manually setting window.process = { env: {} };, but it's a hack.

I'd look into your vue bundler config to see if there is a way to properly include the util in your browser bundle.

I'd highly appreciate you report back your findings for other Vue users!

anttiviljami avatar Feb 21 '21 12:02 anttiviljami

Thanks for trying to help. Your mentioned hack with manually setting does not work either. I'm pretty new to Vite so I have a hard time understanding what's going on there. I will post a solution when I come up with one :) As soon as I remove the import of openapi-client-axios my project continues working. For me openapi-client-axios does not work with Vue3+Vite at the moment, I guess because of dependencies to other projects like util.

binarykiwi avatar Feb 22 '21 10:02 binarykiwi

Similar issue with Vue3 + Quasar2, after installing utils package, the following issues remain outstanding:

App •  ERROR  •  UI  in ./node_modules/@apidevtools/json-schema-ref-parser/lib/resolvers/http.js

Module not found: Can't resolve imported dependency "http"
Did you forget to install it? You can run: npm install --save http

 App •  ERROR  •  UI  in ./node_modules/@apidevtools/json-schema-ref-parser/lib/resolvers/http.js

Module not found: Can't resolve imported dependency "https"
Did you forget to install it? You can run: npm install --save https

Installing those two packages does not work.

PetrPy avatar Jun 15 '21 15:06 PetrPy

You’ll need to provide polyfills for these native nodejs dependencies in your bundler.

See https://cli.vuejs.org/guide/browser-compatibility.html#polyfills for more details

anttiviljami avatar Jun 16 '21 06:06 anttiviljami

You’ll need to expand your webpack config and add fallbacks for the missing node dependencies.

https://cli.vuejs.org/guide/webpack.html#simple-configuration

Also see https://stackoverflow.com/questions/64557638/how-to-polyfill-node-core-modules-in-webpack-5

Hope this gets you on the right track! 😊

anttiviljami avatar Jun 16 '21 06:06 anttiviljami

You’ll need to expand your webpack config and add fallbacks for the missing node dependencies.

https://cli.vuejs.org/guide/webpack.html#simple-configuration

Also see https://stackoverflow.com/questions/64557638/how-to-polyfill-node-core-modules-in-webpack-5

Hope this gets you on the right track! 😊

Well, thanks, but it actually did not help. After spending 3 hours trying to work it out, I gave up... There does not seem to be a straightforward way for Vue.js developers to use openapi-client-axios.

PetrPy avatar Jun 16 '21 11:06 PetrPy

Yeah, looks like Vite doesn't love "node.js" modules, so, I found one dirty solution:

  1. Add into vite.config.ts an define
export default defineConfig({
  define: {
    'process.browser': 'true',
  },
})
  1. Put into your index.html one fake-polyfill for Buffer (before main script src section)
    <script>
      Buffer = {isBuffer: () => {return false} }
    </script>
  1. Then install one required module npm install url

  2. And at last make some magic

import axios from 'axios';

import OpenAPIClientAxios from 'openapi-client-axios';
import { Client as PetStoreClient } from './types';

async function getClient() {
    // (!) Download scheme by yourself or read from file
    // because when you pass in OACA an url, 
    // it make some calls using fns from node native `http` and `https` libs
    const definition = (await axios.get('http://127.0.0.1:3000/docs/json')).data
    
    // Pass it into OACA
    const api = new OpenAPIClientAxios({ 
        withServer: {url:'http://127.0.0.1:3000'},
        definition: definition
    });

    // Voila!
    const client = await api.init<PetStoreClient>();
    const result = await client.createDog(undefined, {name:'test'})
    console.log('result', result.data)
}

@anttiviljami, also I think that we can help someone with this, if will be building ESM or .min version, where all node-core-pollyfills will be already included.

(I can't found for Vite any adequate node-core polyfill plugin too)

npdev453 avatar Sep 15 '21 02:09 npdev453

Same issue, solved by adding this into vue.config.js :

const NodePolyfillPlugin = require( 'node-polyfill-webpack-plugin' );

module.exports = {
  configureWebpack: {
    plugins: [
      new NodePolyfillPlugin(),
    ],
  },
};

sign0 avatar Jan 05 '22 20:01 sign0

This issue should be fixed with [email protected]

anttiviljami avatar Jan 14 '23 10:01 anttiviljami