Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why package.json in each component folder?

See original GitHub issue

Why does each component get its own package.json, like this?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

jzelenkovcommented, Nov 19, 2015

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.

koistyacommented, Nov 19, 2019

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"
Read more comments on GitHub >

github_iconTop Results From Across the Web

package.json - npm Docs
json which is a map of command name to local file name. When this package is installed globally, that file will be either...
Read more >
Anatomy of a Package - SurviveJS
All packages come with a package.json file that contains package metadata, like information about the author, link to a bug tracker and package...
Read more >
Publish Multiple Components to NPM with no package.jsons ...
json file for each of your components. The package.json file can be found in your component's folder inside your node_modules directory ( ...
Read more >
(Ab)using package.json over index.js | by axelnormand
Millions of package.json I find easier to ignore than millions of index.js files :) One less js file in each folder!
Read more >
node.js - Package.json - set main directory - Stack Overflow
It's documentation for a set of APIs, so each API is exported from its own sub-directory. There is no central API info that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found