npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

package-json-from-dist

v1.0.0

Published

Load the local package.json from either src or dist folder

Downloads

62,081,776

Readme

package-json-from-dist

Sometimes you want to load the package.json into your TypeScript program, and it's tempting to just import '../package.json', since that seems to work.

However, this requires tsc to make an entire copy of your package.json file into the dist folder, which is a problem if you're using something like tshy, which uses the package.json file in dist for another purpose. Even when that does work, it's asking the module system to do a bunch of extra fs system calls, just to load a version number or something. (See this issue.)

This module helps by just finding the package.json file appropriately, and reading and parsing it in the most normal fashion.

Caveats

This only works if your code builds into a target folder called dist, which is in the root of the package. It also requires that you do not have a folder named node_modules anywhere within your dev environment, or else it'll get the wrong answers there. (But, at least, that'll be in dev, so you're pretty likely to notice.)

If you build to some other location, then you'll need a different approach. (Feel free to fork this module and make it your own, or just put the code right inline, there's not much of it.)

USAGE

// src/index.ts
import { findPackageJson, loadPackageJson } from 'package-json-from-dist'

const pj = findPackageJson(import.meta.url)
console.log(`package.json found at ${pj}`)

const pkg = loadPackageJson(import.meta.url)
console.log(`Hello from ${pkg.name}@${pkg.version}`)

If your module is not directly in the ./src folder, then you need to specify the path that you would expect to find the package.json when it's not built to the dist folder.

// src/components/something.ts
import { findPackageJson, loadPackageJson } from 'package-json-from-dist'

const pj = findPackageJson(import.meta.url, '../../package.json')
console.log(`package.json found at ${pj}`)

const pkg = loadPackageJson(import.meta.url, '../../package.json')
console.log(`Hello from ${pkg.name}@${pkg.version}`)

When running from CommmonJS, use __filename instead of import.meta.url.

// src/index.cts
import { findPackageJson, loadPackageJson } from 'package-json-from-dist'

const pj = findPackageJson(__filename)
console.log(`package.json found at ${pj}`)

const pkg = loadPackageJson(__filename)
console.log(`Hello from ${pkg.name}@${pkg.version}`)

Since tshy builds both CommonJS and ESM by default, you may find that you need a CommonJS override and some //@ts-ignore magic to make it work.

src/pkg.ts:

import { findPackageJson, loadPackageJson } from 'package-json-from-dist'
//@ts-ignore
export const pkg = loadPackageJson(import.meta.url)
//@ts-ignore
export const pj = findPackageJson(import.meta.url)

src/pkg-cjs.cts:

import { findPackageJson, loadPackageJson } from 'package-json-from-dist'
export const pkg = loadPackageJson(__filename)
export const pj = findPackageJson(__filename)