dai-shi / react-hooks-global-state

[NOT MAINTAINED] Simple global state for React with Hooks API without Context API
https://www.npmjs.com/package/react-hooks-global-state
MIT License
1.1k stars 62 forks source link

About module field in package.json #11

Closed AllenFang closed 5 years ago

AllenFang commented 5 years ago

Hello @dai-shi, firstly, thanks for this awesome package, I just encounter a wired issue when I adopt this package. After deeply digging, i felt the root cause is about the "module field in package.json" probably.

The issue is when I running my application,I saw this error message:

./node_modules/react-hooks-global-state/src/index.js 68:6
Module parse failed: Unexpected token (68:6)
You may need an appropriate loader to handle this file type.
|   const setGlobalState = (name, update) => {
|     globalState = {
>       ...globalState,   <<<  something wrong here
|       [name]: updateValue(globalState[name], update),
|     };

My application stack is

The wired thing is when I use npm will fix this issue. So I try to clone this code and use npm link to do some local testing, I found if I remove module field in package.json or change it to dist/index.js will fix this issue certainly.

Seems like module is still proposal and not sure if there's any different when developer use yarn or npm install the package.

NOTE: You may aware that above error is from the source code(src) instead of dist in the runtime

Could we fix this issue just remove the module field or change it to dist/index.js

Thanks

dai-shi commented 5 years ago

@AllenFang Thanks for reporting this. I assumed that bundler that uses module can deal esnext, but it's not true in your case. So, CRA is fine, but not for next.js. Let me take some time to investigate it and deal with it.

Meanwhile, you can import dist directly. Hope it helps for your use case.

import { createGlobalState } from 'react-hooks-global-state/dist';
AllenFang commented 5 years ago

you can import dist directly. Hope it helps for your use case.

oh, I forget I can do it 😄

Anyway, thanks your reply, you can decide to close this issue anytime.

dai-shi commented 5 years ago

See also: https://github.com/zeit/next.js/issues/706

I'd like to keep "module", unless we find a better way.