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

ReferenceError: require is undefined #44

Closed
coryvirok opened this issue Mar 2, 2023 · 6 comments
Closed

ReferenceError: require is undefined #44

coryvirok opened this issue Mar 2, 2023 · 6 comments
Labels

Comments

@coryvirok
Copy link

For anyone else running into this issue -

While attempting to use this module from an ES6 module, (type: "module" in package.json) I get the following error:

ReferenceError: require is not defined
    at /@fs/Users/dwight/Development/project/node_modules/.pnpm/farmhash@3.2.2/node_modules/farmhash/index.js:3:18
    at instantiateModule 

This corresponds to the require code here -

return require('./build/Release/farmhash.node');

I attempted to add a type: "commonjs" key to the farmhash package.json but that didn't help.

I was able to get past the error by importing farmhash using:

// lib/vendor/farmhash.ts
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const farmhash = require('farmhash')

export default farmhash
// app.ts

import farmhash from './lib/vendor/farmhash'

// ...

I'm not too sure how to fix the issue since I was unable to get the package to build in my fork. But hopefully this helps.

@lovell
Copy link
Owner

lovell commented Mar 2, 2023

Are you using a code bundler such as webpack or esbuild? If so, add farmhash to its external(s) config.

@lovell
Copy link
Owner

lovell commented Jun 2, 2023

According to #44, ensuring farmhash is included in externals does not fix this.

If you're using webpack 5, are you also specifying externalsType?

https://webpack.js.org/configuration/externals/#externalstypenode-commonjs

If you're still having problems, please can you share webpack configuration, ideally as a small repo with a minimal set of configuration that allows someone else to reproduce.

@LadyAiss
Copy link

LadyAiss commented Jun 7, 2023

Hello @lovell,
Thank you for the reply.
I have added externalsType as well but it didn't fix it.

module.exports = {
  // ...
  externalsType: 'commonjs',
 externals: {
    'farmhash': 'commonjs farmhash'
  }
};

@tomplex
Copy link

tomplex commented Aug 7, 2023

hey @lovell, do you have any other recommendations about how to solve this? We're also running into this issue, our build is just installing with npm install and building with tsc with a target of ES2020 and module commonjs. Not sure how / if this is related, but it's only happening for developers with macs, not those running linux.

@lovell
Copy link
Owner

lovell commented Aug 7, 2023

A small repo with a minimal set of configuration that allows someone else to reproduce would be very useful.

@lovell
Copy link
Owner

lovell commented Apr 17, 2024

Closing due to inactivity but, if further help is required, please feel free to reopen with a minimal set of configuration that allows someone else to reproduce

@lovell lovell closed this as completed Apr 17, 2024
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

4 participants