routes-gen: Invalid driver package name or file path.

Describe the bug

I installed the dependencies using npm add routes-gen @routes-gen/remix and run routes-gen -d @routes-gen/remix and I get the following error

CleanShot 2023-01-09 at 02 02 38

Your Example Website or App

Private Repo

Steps to Reproduce the Bug or Issue

  1. Install dependencies with npm
  2. Run the script with npm

Expected behavior

Generate the routes

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 108.0

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (10 by maintainers)

Most upvoted comments

Hi, sorry for the delay in getting back to you.

I think I’ve narrowed down the issue now thanks to the console log from your first comment.

I have set "type":"module", in my package.json file, which means all files with the .js extension are treated as ESM.

require() of ES Module /Users/mikey/Websites/Personal/mikeybinns/remix.config.js from /Users/mikey/Websites/Personal/mikeybinns/node_modules/@routes-gen/remix/dist/index.js not supported.
remix.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename remix.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/mikey/Websites/Personal/mikeybinns/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

I tried changing the file extension as the error message mentions (because the contents is definitely commonjs) but then I encounter this issue:

Error: Cannot find module '/Users/mikey/Websites/Personal/mikeybinns/remix.config.js'

Since v1.6.8, remix.config supports both cjs and mjs file extensions, so I would recommend checking for each of these extensions before you throw an error because of a missing config file.

I believe by adding support for these file extensions, it should resolve our issue. (@danestves don’t forget to changes your file extensions to .cjs if they contain module.exports, or update the code inside the file to ESM code).

If your source code is already ESM, you can import cjs and ESM the same way. It’s that easy.

If your source code is using cjs and you don’t want to convert it to esm, you won’t be able to use require, instead you have to use dynamic import. It’s probably not as complex as you think, this article explains it pretty well. https://adamcoster.com/blog/commonjs-and-esm-importexport-compatibility-examples

In short, you can try to require it in a try catch block, and if that fails, import it instead.