最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - module.exports Cannot set property of undefined - Stack Overflow

programmeradmin1浏览0评论

I am trying to move all react ponents to separate repo so that they can be shared. I am getting the above error

ui-react/index.js

'use strict';

import Button from "./ponents/Button/Button";

module.exports.Button = Button;

FileB

import  {Button} from  'ui-react';

When i do this i get an error called.

'ui-react' does not contain an export named 'Button' 

PS: In package.json of ui-react i have set main to index.js and i have also tried import "ui-react/index"

I am trying to move all react ponents to separate repo so that they can be shared. I am getting the above error

ui-react/index.js

'use strict';

import Button from "./ponents/Button/Button";

module.exports.Button = Button;

FileB

import  {Button} from  'ui-react';

When i do this i get an error called.

'ui-react' does not contain an export named 'Button' 

PS: In package.json of ui-react i have set main to index.js and i have also tried import "ui-react/index"

Share Improve this question asked Nov 15, 2017 at 17:21 aWebDeveloperaWebDeveloper 38.4k41 gold badges177 silver badges247 bronze badges 5
  • 2 Why are you using module.exports instead of export { Button };? Usually mixing module formats is bad idea. – loganfsmyth Commented Nov 15, 2017 at 17:23
  • 1 lack of knowlege – aWebDeveloper Commented Nov 15, 2017 at 17:26
  • Fair enough! :) – loganfsmyth Commented Nov 15, 2017 at 17:32
  • can you share more links on this or enlighten me – aWebDeveloper Commented Nov 15, 2017 at 17:34
  • 1 You could read through 2ality./2014/09/es6-modules-final.html or any number of other posts about ES6 module syntax. require and module.exports are part of the alternative CommonJS module format. The main thing is that you shouldn't really use both together. Some build tooling does allow it, but it usually leads to confusion like this. – loganfsmyth Commented Nov 15, 2017 at 18:22
Add a ment  | 

1 Answer 1

Reset to default 7

No need to write module.exports. You can write

export { Button };
发布评论

评论列表(0)

  1. 暂无评论