Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'path' imported from
moltar opened this issue · comments
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
mmmm, what version of nodejs do you have? 🤔
v14.17.6
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
openapi-codegen/cli/package.json
Lines 8 to 10 in fad67de
This would definitely install openapi-codegen
into node_modules/.bin/
openapi-codegen/cli/package.json
Lines 8 to 10 in fad67de
This would definitely install
openapi-codegen
intonode_modules/.bin/
Indeed, but openapi-codegen
package also, and the bin name is not the same as the package name 😕
❯ 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";
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
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 badWhat 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 ^^
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! ^^
- I installed
@openapi-codegen/cli
,@openapi-codegen/typescript
- I'm in the project root
- I run
npx openapi-codegen
(which just runsnode_modules/.bin/openapi-codegen
(which is your package)
❯ 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?
openapi-codegen/cli/package.json
Lines 8 to 10 in fad67de
This would definitely install
openapi-codegen
intonode_modules/.bin/
As I pointed out here. Your package specifies to install openapi-codegen
binary ...
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 ;)
Which then gets run thru npx openapi-codegen
You are right, it works with 16!
But I cannot upgrade to 16 yet 😢
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
…)
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…
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.
I do have a working version! using slash()
everywhere (my lifesaver package for this kind of problem ^^) I will open a PR
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!
Thanks for fixing so quickly!!
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.
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!
Btw, just a tip, you can also write it like this:
openapi-codegen/cli/src/commands/GenerateCommand.ts
Lines 4 to 14 in 2105e99
import { posix as path } from 'path'
Btw, just a tip, you can also write it like this:
openapi-codegen/cli/src/commands/GenerateCommand.ts
Lines 4 to 14 in 2105e99
import { posix as path } from 'path'
Indeed! will include this in my next PR, thanks for the feedback!