Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import syntax error with express.Router() import #5532

Closed
cainbryce208 opened this issue Mar 13, 2024 · 4 comments
Closed

Import syntax error with express.Router() import #5532

cainbryce208 opened this issue Mar 13, 2024 · 4 comments
Labels

Comments

@cainbryce208
Copy link

Need help figuring out why this would error.

I am using bun and not node for the runtime. I don't think that affects anything here. I am using express JS 5.0 BETA 1
I just feel there is a better way than actually doing (despite Node/Bun caching modules I still feel like this is redundant):

import express from 'express';
const viewRouter = express.Router();
  • The Code
import { Router as viewRouter } from 'express';
viewRouter.get('/', (req, res) => {
    res.render('index.ejs');
});
module.exports = viewRouter;
  • The Error
SyntaxError: Unexpected token '{'. import call expects one or two arguments.
      at <parse> (/mnt/z/sic-web/src/server/routes/viewRouter.js:1:1)
1 | import {Router as viewRouter} from 'express';
    ^
@cainbryce208
Copy link
Author

One thing I made an oopsie here with is the module.exports= viewRouter line which is commonJS .
Change it to default export viewRouter and I get the following error:

TypeError: argument handler must be a function
      at /mnt/z/sic-web/src/node_modules/router/lib/route.js:211:15
      at /mnt/z/sic-web/src/node_modules/express/lib/application.js:474:5
      at /mnt/z/sic-web/src/sicapp.js:11:1
206 | 
207 |     for (var i = 0; i < callbacks.length; i++) {
208 |       var fn = callbacks[i]
209 | 
210 |       if (typeof fn !== 'function') {
211 |         throw new TypeError('argument handler must be a function')
                    ^
TypeError: argument handler must be a function
      at /mnt/z/sic-web/src/node_modules/router/lib/route.js:211:15
      at /mnt/z/sic-web/src/node_modules/express/lib/application.js:474:5
      at /mnt/z/sic-web/src/sicapp.js:11:1
^C
cain@cain-pc:/mnt/z/sic-web/src$ bun run dev
$ bun --hot sicapp.js
206 | 
207 |     for (var i = 0; i < callbacks.length; i++) {
208 |       var fn = callbacks[i]
209 | 
210 |       if (typeof fn !== 'function') {
211 |         throw new TypeError('argument handler must be a function')
                    ^
TypeError: argument handler must be a function
      at /mnt/z/sic-web/src/node_modules/router/lib/route.js:211:15
      at /mnt/z/sic-web/src/node_modules/express/lib/application.js:474:5
      at /mnt/z/sic-web/src/sicapp.js:12:1
206 | 
207 |     for (var i = 0; i < callbacks.length; i++) {
208 |       var fn = callbacks[i]
209 | 
210 |       if (typeof fn !== 'function') {
211 |         throw new TypeError('argument handler must be a function')
                    ^
TypeError: argument handler must be a function
      at /mnt/z/sic-web/src/node_modules/router/lib/route.js:211:15
      at /mnt/z/sic-web/src/node_modules/express/lib/application.js:474:5
      at /mnt/z/sic-web/src/sicapp.js:12:1

@wesleytodd
Copy link
Member

I am nearly positive even if you get this issue worked out you will just hit other issues. Express couples deeply to Node.js api's which are likely different enough to matter. Just don't want to provide an answer on the errors without pointing out you are likely in for a world of pain 🤣. Additionally I don't think it has been discussed yet, but we likely do not have any plans to support Bun, so YMMV on this effort even in the longer term.

That said, it looks to me like something else is up with your code as I am not sure how the runtime difference could cause a typeof check to fail. And since that is the case, I am going to close this since we do not do technical support in here. If you can prove this is a bug of some sort in express please re-open this, but otherwise I would ask on Reddit or StackOverflow.

@joeyguerra
Copy link

I agree with Wes.

I'll add this to give you some suggestion though.

import { Router as viewRouter } from 'express'
viewRouter().get('/', (req, res) => {
  res.render('index')
})
export default viewRouter

Router is a function.

I would suggest the following instead for viewRouter.js

export default app => {
  app.get('/', (req, res) => {
    res.render('index')
  })
}

and in sicapp.js,

import express from 'express'
import viewRouter from './routes/viewRouter.js'
// other code
...

const app = express()
....
viewRouter(app)

@cainbryce208
Copy link
Author

Thank you both, I appreciate the feedback :D
Wes, not a problem. I'm not sure there is any particular use in supporting bun anyways as they either have wrappers or have their own implementation of node libraries. I think what it was though is the file encoding got messed up and maybe lost it from there? My suspicion is a formatter in VsCode.

Joey, this is an excellent alternative to what I was trying to do and even cleaner at that. I will have to take a gander at this when I'm in front of my editor again. Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants