fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'path' imported from

moltar opened this issue · comments

commented

Keep getting the following error:

npx openapi-codegen

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'path' imported from /.../node_modules/@openapi-codegen/cli/lib/commands/GenerateCommand.js

Any ideas?

mmmm, what version of nodejs do you have? 🤔

Wait! this is not my package ^^

This is mine:

 $ npx @openapi-codegen/cli --help
commented

mmmm, what version of nodejs do you have? 🤔

v14.17.6

commented

Wait! this is not my package ^^

🤔 hm, interesting.

Because before install your package that command did not exist, but when I installed it, then it exists but fails.

Will try npx @openapi-codegen/cli

Since I didn't have the time yet to do the init command, you can install @openapi-codegen/cli and @openapi-codegen/typescript and follow this readme https://github.com/fabien0102/openapi-codegen/tree/main/cli

commented

"bin": {
"openapi-codegen": "lib/cli.js"
},

This would definitely install openapi-codegen into node_modules/.bin/

"bin": {
"openapi-codegen": "lib/cli.js"
},

This would definitely install openapi-codegen into node_modules/.bin/

Indeed, but openapi-codegen package also, and the bin name is not the same as the package name 😕

commented
❯ which openapi-codegen
node_modules/.bin/openapi-codegen
❯ head -n5 node_modules/.bin/openapi-codegen
#!/usr/bin/env node
import { Cli } from "clipanion";
import { GenerateCommand } from "./commands/GenerateCommand.js";
import { InitCommand } from "./commands/InitCommand.js";
import { readFileSync } from "fs";
commented

Since I didn't have the time yet to do the init command, you can install @openapi-codegen/cli and @openapi-codegen/typescript and follow this readme https://github.com/fabien0102/openapi-codegen/tree/main/cli

Ok, that's what I did. And the original issue is the result of that :)

Ah, you did npx in a folder where the package was installed! I didn't test the same, my bad

I have node 16.13.1 locally, let me try with the 14

commented

Ah, you did npx in a folder where the package was installed! I didn't test the same, my bad

What do you mean by that?

What's the proper solution?

I think maybe a quick demo repo with the example of the correct setup would be great in this case 😁 🙏🏼

Ah, you did npx in a folder where the package was installed! I didn't test the same, my bad

What do you mean by that?

What's the proper solution?

I think maybe a quick demo repo with the example of the correct setup would be great in this case 😁 🙏🏼

I'm totally with you, and this is in my todolist since a long time 😅 but time… time…

I meant I was trying npx in a random folder for reproducing, so this was using openapi-codegen instead of my package ^^, but if you install a package and after run npx this is actually working 😉 sorry for the mess ^^

commented

But openapi-codegen is your package 😁

... so I don't understand?

But openapi-codegen is your package 😁

... so I don't understand?

https://www.npmjs.com/package/openapi-codegen <- this one is not! ^^

commented
  1. I installed @openapi-codegen/cli, @openapi-codegen/typescript
  2. I'm in the project root
  3. I run npx openapi-codegen (which just runs node_modules/.bin/openapi-codegen (which is your package)
commented
❯ head -n5 node_modules/.bin/openapi-codegen
#!/usr/bin/env node
import { Cli } from "clipanion";
import { GenerateCommand } from "./commands/GenerateCommand.js";
import { InitCommand } from "./commands/InitCommand.js";
import { readFileSync } from "fs";

Isn't that your package?

commented

"bin": {
"openapi-codegen": "lib/cli.js"
},

This would definitely install openapi-codegen into node_modules/.bin/

As I pointed out here. Your package specifies to install openapi-codegen binary ...

commented

Which gets installed into node_modules/.bin/openapi-codegen

You are actually in the correct package, I was not when I tried to repro with npx, give me 10mn to try with node 14, since I was force to use the latest module feature for fetch, this is maybe why ;)

commented

Which then gets run thru npx openapi-codegen

commented

You are right, it works with 16!

commented

But I cannot upgrade to 16 yet 😢

image

And you are right, this is not working with 14 😖

commented

It works like this:

- import path from "path/posix";
+ import path from "path";

It works like this:

- import path from "path/posix";
+ import path from "path";

Interesting… let's see if this is still working on windows with this change (my god I hate this path…)

image

Sorry, you will be blocked just after! maybe with some nodejs flags somewhere… let me check

commented

Oh, I see what you are doing. You are crafting the path, and then using dynamic imports to load the config.

Yeah, this is to have a typescript config (so typesafe with autocompletion), but everything have a price, this was hard 😅 but the problem is the .mjs due to the dependency got (to retrieve distant specs)

You are rigeht, the problem is path indeed, I didn't pay attention to the backslash in my path…

commented

ok, this should work cross platform?

import path from "path";

path.posix(...)

I think the reason why

import path from "path/posix";

does not work, is because in older node version there was no path/posix file. Maybe it was just exporting posix object from the root file.

commented

Yeah, this is to have a typescript config (so typesafe with autocompletion), but everything have a price, this was hard 😅 but the problem is the .mjs due to the dependency got (to retrieve distant specs)

screenshot-20220222T194208-uNg02P49

Works with TS too

I do have a working version! using slash() everywhere (my lifesaver package for this kind of problem ^^) I will open a PR

commented

I think using posix is fine, and I think slash might not be needed. Just need to use posix like this:

import path from "path";

path.posix.join(...)

Let's try!

Time to wait for the CI to release a new version and try 😁

Thanks for reporting this bug!

commented

Thanks for fixing so quickly!!

commented

It works!! 🎉

Thanks!

Have fun! Don't hesitate if you have any question! I really need to find more time to write more docs & examples.

commented

I really need to find more time to write more docs & examples.

Open some issues with what you have in mind and tag me, maybe I can help!

commented

Btw, just a tip, you can also write it like this:

import _path from "path";
import * as swc from "@swc/core";
import prettier from "prettier";
import { fileURLToPath } from "url";
import slash from "slash";
import { Config, FromOptions, Namespace } from "../types";
import { getOpenAPISourceFile } from "../core/getOpenAPISourceFile.js";
import { parseOpenAPISourceFile } from "../core/parseOpenAPISourceFile.js";
const path = _path.posix;

import { posix as path } from 'path'

Btw, just a tip, you can also write it like this:

import _path from "path";
import * as swc from "@swc/core";
import prettier from "prettier";
import { fileURLToPath } from "url";
import slash from "slash";
import { Config, FromOptions, Namespace } from "../types";
import { getOpenAPISourceFile } from "../core/getOpenAPISourceFile.js";
import { parseOpenAPISourceFile } from "../core/parseOpenAPISourceFile.js";
const path = _path.posix;

import { posix as path } from 'path'

Indeed! will include this in my next PR, thanks for the feedback!