Require().default works but import does not

66 views Asked by At

I'm attempting to use @as-integrations/hapi in a JavaScript project with the import system. I've followed the example and I keep getting the same error

import hapiPlugin from '@as-integrations/hapi';
# also tried
import * as integrations from '@as-integrations/hapi';

# terminal output
const name = item.plugin.name ?? item.plugin.pkg.name;
TypeError: Cannot read properties of undefined (reading 'name')

However, if I revert to using the require syntax it works just fine.

const hapiApollo = require('@as-integrations/hapi').default;

I have updated my package.json with the following configs

"exports": "./index.js", # where I'm requiring/importing from
"type": "module",

What is the difference here? Why does require().default work but import does not?

UPDATE: The error is coming from node_modules/@hapi/hapi/lib/server.js where it tries to load the plugin from @as-integrations/hapi. It isn't able to locate the pkg or name inside the plugin I'm passing in

if (!item.plugin) {
    item = {
        plugin: item
    };
}
else if (!item.plugin.register) {
    item = {
        options: item.options,
        once: item.once,
        routes: item.routes,
        plugin: item.plugin.plugin
    };
}
else if (typeof item === 'function') {
    item = Object.assign({}, item);         // Shallow cloned
}

item = Config.apply('plugin', item);

const name = item.plugin.name ?? item.plugin.pkg.name;
0

There are 0 answers