preactjs / preact-cli

😺 Your next Preact PWA starts in 30 seconds.
MIT License
4.69k stars 375 forks source link

"Uncaught ReferenceError: process is not defined" when importing Influx #596

Closed dandv closed 6 years ago

dandv commented 6 years ago

Bug report

What is the current behaviour?

Importing the influx package in src/components/app.js causes this error in the browser:

image

Influx is not a React package, yet it seems impossible to use influx in preact apps. It can be imported without issue in a React app.

If the current behaviour is a bug, please provide the steps to reproduce.

preact create default preact-influx
cd preact-influx
npm install influx
# edit src/component/app.js and add an `import Influx from 'influx';` line
npm run start

What is the expected behaviour?

This works in React:

npx create-react-app react-influx
cd react-influx
npm install influx
# edit src/App.js and add an `import Influx from 'influx';` line
yarn start

Please mention other relevant information.

lukeed commented 6 years ago

I think you mean to use the browser version: https://node-influx.github.io/manual/usage.html#browser-setup

The error says it's expecting process, which means it's a Node.js library. This is 100% something you don't want to be shimmed on the browser. CRA allows this mistake.

lukeed commented 6 years ago

I've never used this library, so if it still complains about wanting process, here's how you enable it:

// preact.config.js

module.exports = config => {
  config.node.process = true
}

https://github.com/developit/preact-cli/blob/6742f94664cf88ca0159f809e715fd441495e653/src/lib/webpack/webpack-base-config.js#L245

dandv commented 6 years ago

I've tried that preact.config.js setting, and now I get "Buffer is not defined".

Here's a repo showing that influx can be used in the browser apparently without any shimming. It's reduced to the minimum from a project in which I am able to actually use Influx in the browser to retrieve data from a database.

lukeed commented 6 years ago

Okay. You can do the same with Buffer. You'll see all the node shims we disable in that link I provided.

dandv commented 6 years ago

Didn't realize that webpack shimmed process and Buffer by default. Thanks!

lukeed commented 6 years ago

No problem!