Adminjs implementation issue "Cannot use import statement outside a module"

265 views Asked by At

I'm new in node express js. Just trying to writting the backend code using express js. Now I want to add adminjs in my project. As I'm new in this section, I've followed the official documentation of adminjs express plugin https://docs.adminjs.co/installation/plugins/express

But I'm facing issue while running app.js

import AdminJSExpress from '@adminjs/express'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1287:20)
    at Module._compile (node:internal/modules/cjs/loader:1339:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1434:10)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Module._load (node:internal/modules/cjs/loader:1022:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49

Node.js v21.5.0

I have gone through the chatGPT. it suggest me to add "type": module in package.json. but I don't want to change it to module. I also don't want to use typescript

I just want to add admin panel in my existing project which is frogbase using express.js and postgresql

If you want you can also checkout the github repository https://github.com/frogbase/frogbase

1

There are 1 answers

3
jQueeny On BEST ANSWER

I don't use @adminjs/express so not 100% sure this will work for you but looking at its ES export from npm and the AdminJS Express simple example you can try this:

const express = require('express');

Promise.all([
    import('adminjs'),
    import('@adminjs/express'),
]).then(([{default: AdminJS}, {default: AdminJSExpress}]) => {
    const PORT = 3000
    const app = express()

    const admin = new AdminJS({})

    const adminRouter = AdminJSExpress.buildRouter(admin)
    app.use(admin.options.rootPath, adminRouter)

    app.listen(PORT, () => {
        console.log(`AdminJS started on http://localhost:${PORT}${admin.options.rootPath}`)
    })
});