-1

I am trying to add my local custom Nuxt module to my Nuxt application and I keep getting the error:

Cannot read property 'startsWith' of undefined                                                                                                

  at Resolver.g [as _require] (node_modules\jiti\dist\jiti.js:1:54052)
  at Resolver.requireModule (node_modules\@nuxt\core\dist\core.js:381:29)
  at ModuleContainer.addModule (node_modules\@nuxt\core\dist\core.js:182:38)
  at node_modules\@nuxt\utils\dist\utils.js:639:43
  at async ModuleContainer.ready (node_modules\@nuxt\core\dist\core.js:55:5)
  at async Nuxt._init (node_modules\@nuxt\core\dist\core.js:478:5)

I require 'path' as below in module.js of my Nuxt module:

const { resolve, join } = require('path')
const { readdirSync } = require('fs')

It displays a list of files that has startsWith(). For instance:

node_modules@nuxt\utils\dist\utils.js:

const startsWithAlias = aliasArray => str => aliasArray.some(c => str.startsWith(c));
...
const isWindows = process.platform.startsWith('win');
...
if (r.path && r.path.startsWith('/')) {
      routes.push(r.path);
    } else if (route !== '/' && route[route.length - 1] === '/') {
      routes.push(route.slice(0, -1));
    } else {
      routes.push(route);
    }
...
const sanitizedKey = key.startsWith('_') ? key.substr(1) : key;
...
if (key.startsWith('_') && key.length > 1) {
          route.path += '?';
        }
...
if (
    dir1 &&
    dir2 &&
    (
      dir1 === dir2 ||
      (
        dir1.startsWith(dir2) &&
        !path__default['default'].basename(dir1).startsWith(path__default['default'].basename(dir2))
      )
    )
  ) {
 ...
 if (key.startsWith('_')) {
    return `:${key.substr(1)}`
  }
 
S.P
  • 29
  • 1
  • 9
  • 2
    can you show us the code that calling startsWith so we can help you debug, there is not enough information here for us to accurately give advice. Have a look here if you need help: https://stackoverflow.com/help/minimal-reproducible-example – about14sheep Nov 14 '21 at 19:47
  • Are you sure that `!path__default['default'].basename(dir1)` is populated and is not null, undefined or alike? – kissu Nov 15 '21 at 13:46

2 Answers2

3

I've struggled with this for some time too – in my case, it was just a dumb mistake. I was missing a comma in the modules section in nuxt.config.js.

le_Machy
  • 96
  • 7
0

Looks like it might be trying to initialise the module on the client so it's not recognising node specific code.

If by chance you're using the nuxt-vite package for nuxt 2, this could potentially be why as I don't think vite-plugin-vue2 which it uses works with SSR yet.

* I understand that may be a weird mix of vague and specific, but going off the question it looks very similar to an issue my teammate had when looking into nuxt-vite. Also, judging by the stack trace it's failing before it even properly gets in to the module.