项目主页: https://github.com/afeiship/read-pkg-json
yarn add @jswork/read-pkg-json
import readPkgJson from '@jswork/read-pkg-json'; // 1. current dir const pkg = readPkgJson(); // 2. specify dir const pkg = readPkgJson('path/to/your/project');
const res = fs.readFileSync('path' + 'package.json', 'utf-8'); return JSON.parse(res)
const pkg = require('./package.json'); console.log(pkg)
import { createRequire } from "module"; import path from "path"; const __dirname = path.dirname(new URL(import.meta.url).pathname); const require = createRequire(__dirname); const pkg = require(path.join(__dirname, "package.json")); console.log(pkg);