Giter Site home page Giter Site logo

Comments (11)

jzelenkov avatar jzelenkov commented on May 21, 2024 24

it's a convenience thing. It allows you to write a require statement for a directory instead of specific file.

without package.json:

import Layout from 'src/components/Layout/Layout.js'
// var Layout = require('src/components/Layout/Layout.js');

with package.json containing main keyword:

import Layout from 'src/components/Layout'
// var Layout = require('src/components/Layout');

Alternatively, one could rename Layout.js to index.js. Then require-statement resolution would work without package.json as well.

from react-firebase-starter.

koistya avatar koistya commented on May 21, 2024 2

One of the use cases where package.json can be very helpful, is when you need to create an isomorphic component, having different implementations for Node.js and Browser environments:

{
  "name": "MyComponent",
  "version": "0.0.0",
  "private": true,
  "main": "./MyComponent.browser.js",
  "node": "./MyComponent.node.js"
}

from react-firebase-starter.

dehghani-mehdi avatar dehghani-mehdi commented on May 21, 2024 2

When I put package.json inside component's folder, I got Support for the experimental syntax 'jsx' isn't currently enabled error, without package.json everything works perfectly, anyone knows how I can fix this?

btw I don't use any of kriasoft's kits.

from react-firebase-starter.

dsernst avatar dsernst commented on May 21, 2024 1

Interesting. Thank you

from react-firebase-starter.

alexesDev avatar alexesDev commented on May 21, 2024

Its conflict with https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-extraneous-dependencies.md

'react' should be listed in the project's dependencies. Run 'npm i -S react' to add it (import/no-extraneous-dependencies)

from react-firebase-starter.

koistya avatar koistya commented on May 21, 2024

@alexesDev thanks for heads up! Fixed.

from react-firebase-starter.

krzywiecki avatar krzywiecki commented on May 21, 2024

@jayzelenkov I'm going to use package.json for every component in my app (so far I've been using index.js but I find package.json more natural for it) and I encounter this post. Now I'm wondering why you defined in the package.json also 3 other properties besides "main". Are you using:

"name": "Layout",
 "version": "0.0.0",
 "private": true,

these for anything?

from react-firebase-starter.

koistya avatar koistya commented on May 21, 2024

@krzywiecki just to make it a valid package.json file, otherise the editor would highlight it. But, it seems like using index.js re-exporting public APIs of the component is a more widely adopted approach:

// components/Toolbar/Toolbar.js
export function Toolbar(props) { ... }
// components/Toolbar/index.js
export { Toolbar as default } from './Toolbar';

from react-firebase-starter.

krzywiecki avatar krzywiecki commented on May 21, 2024

@koistya thanks for the answer. Actually my editor is not complaining so I'll stick with "main" property only up until I find a use case for some other prop.

Yeah, it's true that index.js is commonly used for exporting public API of a component but I think it's better to use package.json for it as:

  • having javascript file tend to tempt to put more things there
  • way of exporting can be different (some people do export Toolbar from './Toolbar'; some export default from './Toolbar')

I want to have consistency and also force to export only one thing from a module, that's why I decide to go with package.json instead of index.js

from react-firebase-starter.

krzywiecki avatar krzywiecki commented on May 21, 2024

Yeah, that's a perfect example and I believe there might be more use cases.

from react-firebase-starter.

DmitryOlkhovoi avatar DmitryOlkhovoi commented on May 21, 2024

@koistya
Hi guys, sorry for off-topic.
How I can build my project in such a way? Package.json per component in the dist folder
https://ibb.co/mR51tM9

from react-firebase-starter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.