huan / hot-import

Hot Module Replacement (HMR) for Node.js
https://npmjs.com/package/hot-import
Apache License 2.0
25 stars 2 forks source link

TypeError: is not a function #10

Closed suntong closed 6 years ago

suntong commented 6 years ago

Please take a look at the file https://github.com/suntong/nodecl/blob/master/sbaa/cfg_hot/demo2.js

When I run it, I get

TypeError: is not a function
    at proxyModule (/path/to/node_modules/hot-import/dist/src/hot-import.js:214:19)
    at main (/path/to/nodecl/sbaa/cfg_hot/demo2.js:18:14)

How to fix it? Thx!

huan commented 6 years ago

Please have a look at https://github.com/zixia/hot-import/blob/master/examples/demo-js.ts, it should work without any problem.

After that, you will be able to compare the example with your code and find out which part caused your error.

Good luck!

suntong commented 6 years ago

Actually, I've duplicated it as mine, https://github.com/suntong/nodecl/blob/master/sbaa/cfg_hot/demo1.js

and yes, it works without any problem. However, it is from demo1.js to demo2.js that causes the problem, and as explained in OP, the problem happens at proxyModule at hot-import.js:214:19.

huan commented 6 years ago

So what did you do/change in the demo2 from demo1?

suntong commented 6 years ago

I believe these too lines are making the big differences:

https://github.com/suntong/nodecl/blob/master/sbaa/cfg_hot/demo2.js#L12-L13

huan commented 6 years ago

It seems you have to export a function instead of an object.

try to use a factory function to get your data:

const MODULE_CODE_1 = 'const config = {v: 3}; module.exports = () => return config'
const MODULE_CODE_2 = 'const config = {v: 5}; module.exports = () => config'
suntong commented 6 years ago

That works like a charm! Thanks a lot for your help!

huan commented 6 years ago

BTW: if you change

- const v1 = hotMod()
+ const v1 = hotMod

in your code, you could also fix your problem before.

https://github.com/suntong/nodecl/blob/616160a299850986886e48ed0b6692c73d8db103/sbaa/cfg_hot/demo2.js#L18

suntong commented 6 years ago

Ahh! Yes, I was wondering why I have to use a hotMod() function call earlier, :-) That really make it clear now. Thanks a lot for the follow up!