Gathering detailed insights and metrics for package-json-from-dist
Gathering detailed insights and metrics for package-json-from-dist
Gathering detailed insights and metrics for package-json-from-dist
Gathering detailed insights and metrics for package-json-from-dist
npm install package-json-from-dist
Typescript
Module System
Node Version
NPM Version
99.8
Supply Chain
99.5
Quality
76
Maintenance
100
Vulnerability
100
License
TypeScript (99.51%)
JavaScript (0.49%)
Total Downloads
1,025,027,810
Last Day
1,789,530
Last Week
31,838,873
Last Month
137,206,180
Last Year
1,025,026,783
NOASSERTION License
9 Stars
6 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Feb 25, 2025
Minified
Minified + Gzipped
Latest Version
1.0.1
Package Id
package-json-from-dist@1.0.1
Unpacked Size
35.60 kB
Size
5.62 kB
File Count
13
NPM Version
10.7.0
Node Version
20.13.1
Published on
Sep 26, 2024
Cumulative downloads
Total Downloads
Last Day
-8.9%
1,789,530
Compared to previous day
Last Week
-7.3%
31,838,873
Compared to previous week
Last Month
0.3%
137,206,180
Compared to previous month
Last Year
99,807,765.9%
1,025,026,783
Compared to previous year
6
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.
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.)
1// src/index.ts 2import { 3 findPackageJson, 4 loadPackageJson, 5} from 'package-json-from-dist' 6 7const pj = findPackageJson(import.meta.url) 8console.log(`package.json found at ${pj}`) 9 10const pkg = loadPackageJson(import.meta.url) 11console.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.
1// src/components/something.ts 2import { 3 findPackageJson, 4 loadPackageJson, 5} from 'package-json-from-dist' 6 7const pj = findPackageJson(import.meta.url, '../../package.json') 8console.log(`package.json found at ${pj}`) 9 10const pkg = loadPackageJson(import.meta.url, '../../package.json') 11console.log(`Hello from ${pkg.name}@${pkg.version}`)
When running from CommmonJS, use __filename
instead of
import.meta.url
.
1// src/index.cts 2import { 3 findPackageJson, 4 loadPackageJson, 5} from 'package-json-from-dist' 6 7const pj = findPackageJson(__filename) 8console.log(`package.json found at ${pj}`) 9 10const pkg = loadPackageJson(__filename) 11console.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
:
1import { 2 findPackageJson, 3 loadPackageJson, 4} from 'package-json-from-dist' 5//@ts-ignore 6export const pkg = loadPackageJson(import.meta.url) 7//@ts-ignore 8export const pj = findPackageJson(import.meta.url)
src/pkg-cjs.cts
:
1import { 2 findPackageJson, 3 loadPackageJson, 4} from 'package-json-from-dist' 5export const pkg = loadPackageJson(__filename) 6export const pj = findPackageJson(__filename)
No vulnerabilities found.
No security vulnerabilities found.