Error: Cannot find module 'tslib' on adminbro tutorial

7.1k Views Asked by At

I'm trying to build a nodejs app using admin bro.

They have provided a tutorial for Role Based Access example Here

I run these to create a new directory for the app:

mkdir my-admin-app
cd my-admin-app
yarn init -y

To install dependencies, I ran:

yarn add express express-formidable mongoose admin-bro @admin-bro/mongoose @admin-bro/express

This is the example app...

// Requirements
const mongoose = require('mongoose')
const express = require('express')
const AdminBro = require('admin-bro')
const AdminBroExpressjs = require('@admin-bro/express')

// We have to tell AdminBro that we will manage mongoose resources with it
AdminBro.registerAdapter(require('@admin-bro/mongoose'))

// express server definition
const app = express()

// Resources definitions
const User = mongoose.model('User', {
  email: { type: String, required: true },
  password: { type: String, required: true },
  role: { type: String, enum: ['admin', 'restricted'], required: true },
})

// Pass all configuration settings to AdminBro
const adminBro = new AdminBro({
  resources: [User],
  rootPath: '/admin',
})

// Build and use a router which will handle all AdminBro routes
const router = AdminBroExpressjs.buildRouter(adminBro)
app.use(adminBro.options.rootPath, router)

// Running the server
const run = async () => {
  await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true })
  await app.listen(8080, () => console.log(`Example app listening on port 8080!`))
}

run()

To test everything is working as expected...

node index.js

Expected output is this:

AdminBro: bundle ready
Example app listening on port 8080!

But I get this:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'tslib'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)

I came across this question but the imports suggested there are not used in the example app so I think the adminbro library already should include those.

Testing environments where this failed:

  • Local Nodejs version 10 with yarn and npm
  • A new Nodejs version 15 on docker (brand new container)

Thanks

2

There are 2 best solutions below

0
On

This error could happen if you have a js file that requires a tslib but you do not have it installed in the location. For instance, I had issue with ngx-unused-css which I installed it globally and I do not have tslinb on global.

To fix that, run npm install -g tslib

0
On

npm install tslib solved the issue in my casae