sindresorhus / read-pkg-up Goto Github PK
View Code? Open in Web Editor NEWRead the closest package.json file
License: MIT License
Read the closest package.json file
License: MIT License
Using airbnb
+ prettier
for ESlinting, I can't destructure the way I use to when reading a package. I now get this linting error:
Parsing error: The keyword 'package' is reserved
Before I could do
const { pkg } = require('read-pkg-up').sync();
But as of v6 I can't do the following without the above error
const { package } = require('read-pkg-up').sync();
Can pkg
be left for backwards compatibility? Or is there a better recommended way of importing? I guess I can this?
const { package: pkg } = require('read-pkg-up').sync();
Not sure why the property needed to be renamed, but just wanted to call out this issue in case others run into it.
I see Strips UTF-8 BOM
in the README.
But I didn't find anywhere to use it(strip-bom)
https://www.npmjs.com/package/read-pkg
Would you help to bump this package?
I'm recieveing the MergeDeep
recursion error using TypeScript 5.4.0
, which was fixed in the latest version of type-fest
: sindresorhus/type-fest#807, but all dependencies of read-package-up including read-pkg rely on an outdated type-fest
version:
Output of pnpm list type-fest --depth=100
:
dependencies:
read-package-up 11.0.0
├─┬ read-pkg 9.0.1
│ ├─┬ parse-json 8.1.0
│ │ └── type-fest 4.8.3
│ └── type-fest 4.8.3
└── type-fest 4.8.3
This is the current behavior:
// example-module/example-dir/example.js
const readPkg = require('read-pkg')
const readPkgUp = require('read-pkg-up')
const pkgUp = require('pkg-up')
const test = async () => {
console.log('__dirname\n')
console.log(__dirname)
console.log('\n=====================\n')
console.log('pkgUp\n')
await pkgUp(__dirname).then(console.log)
console.log('\n=====================\n')
console.log('pkgUp + readPkg\n')
await pkgUp(__dirname).then(readPkg).then(console.log)
console.log('\n=====================\n')
console.log('readPkgUp\n')
await readPkgUp(__dirname).then(console.log)
}
test()
__dirname
/home/dani/projects/example-module/example-dir
=====================
pkgUp
/home/dani/projects/example-module/package.json
=====================
pkgUp + readPkg
{ name: 'example-module,
version: '1.0.0',
description: 'Example project',
main: 'main.js',
readme: 'ERROR: No README data found!',
_id: '[email protected]' }
=====================
readPkgUp
{}
In my view, readPkgUp
should behave just like pkgUp + readPkg
for consistency, and both executions above should return the correct package.json
object.
This could be implemented as a non-breaking change, because the options object can still work fine but the module could detect whether the input argument is an object or a string and work with both formats.
Support for object destructuring was added by a minor Node@8 version. In order to support Node@8, which I would say is desirable, Object.assign
must be used. This is an issue for this package, but also for its dependency read-pkg
.
The most recent version 8.0.0 is not working.
According to the error messages, following imports are malformed:
https://github.com/sindresorhus/read-pkg-up/blob/e6fbe7f1ce52cbfb924543ef0e9801775efc38cc/index.js#L1-L2
I'm facing following message:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: .\node_modules\read-pkg-up\index.js
require() of ES modules is not supported.
require() of .\node_modules\read-pkg-up\index.js from .\lib\tests\Management\DependencyCollection.test.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from .\node_modules\read-pkg-up\package.json.
at new NodeError (node:internal/errors:329:5)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1109:13)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (.\lib\tests\Management\DependencyCollection.test.js:15:23)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (.\lib\tests\Management\index.js:14:37)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (.\lib\tests\main.test.js:13:22)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:201:29)
at ModuleJob.run (node:internal/modules/esm/module_job:154:23)
at async Loader.import (node:internal/modules/esm/loader:166:24)
at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
at async formattedImport (.\node_modules\mocha\lib\esm-utils.js:7:14)
at async Object.exports.loadFilesAsync (.\node_modules\mocha\lib\esm-utils.js:55:20)
at async singleRun (.\node_modules\mocha\lib\cli\run-helpers.js:125:3)
at async Object.exports.handler (.\node_modules\mocha\lib\cli\run.js:362:5)
There are so very many moving parts. But after converting a project to ESM, I cannot run jest anymore for this package.
Details:
/oss/service/node_modules/read-pkg-up/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import path from 'node:path';
^^^^^^
SyntaxError: Cannot use import statement outside a module
2 |
3 | import dotenv from 'dotenv';
> 4 | import { readPackageUp } from 'read-pkg-up';
| ^
5 | import type { NormalizedPackageJson } from 'read-pkg-up';
You can see it fail here:
https://github.com/openapi-typescript-infra/service/tree/djmax/esm
I also can't eslint anymore, but one thing at a time. I'm mostly making the ESM move because you have so many packages and they are all ESM. I've tried a few times in the past year and failed each time. I'm hoping someone will have pity on me and perhaps I won't fail yet again.
You renamed .package
in 8140c73 .
But why not readPkgUp
and readPkgUp.sync
for the json
, add readPkgUp.resolve
and readPkgUp.resolve.sync
to get the path
, just like require
Using node:latest
bumps into this error:
./node_modules/.bin/nyc --reporter=lcov --reporter=text ./node_modules/.bin/mocha tests --timeout 10000 --recursive --check-leaks --exit --reporter mocha-junit-reporter --reporter-options mochaFile=test_reports/mocha/test-results.xml
node:internal/modules/cjs/loader:1109
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /root/workspace/node_modules/read-pkg-up/index.js
require() of ES modules is not supported.
require() of /root/workspace/node_modules/read-pkg-up/index.js from /root/workspace/node_modules/test-exclude/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /root/workspace/node_modules/read-pkg-up/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /root/workspace/node_modules/read-pkg-up/package.json.
at new NodeError (node:internal/errors:329:5)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1109:13)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (/root/workspace/node_modules/test-exclude/index.js:4:19)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32) {
code: 'ERR_REQUIRE_ESM'
}
Exited with code exit status 1
I cannot use ESM pure versions(>=8) yet, so currently stick with v7.0.1.
And it has a problem that implementation and typing are different.
Its typing is the snippet below.
import {Except} from 'type-fest';
import {readPackage, readPackageSync, Options as ReadPackageOptions, NormalizeOptions as ReadPackageNormalizeOptions, PackageJson, NormalizedPackageJson} from 'read-pkg';
export type Options = {
/**
The directory to start looking for a package.json file.
@default process.cwd()
*/
cwd?: URL | string;
} & Except<ReadPackageOptions, 'cwd'>;
export type NormalizeOptions = {
/**
The directory to start looking for a package.json file.
@default process.cwd()
*/
cwd?: URL | string;
} & Except<ReadPackageNormalizeOptions, 'cwd'>;
export interface ReadResult {
packageJson: PackageJson;
path: string;
}
export interface NormalizedReadResult {
packageJson: NormalizedPackageJson;
path: string;
}
export {
PackageJson,
NormalizedPackageJson,
};
/**
Read the closest `package.json` file.
@example
``
import {readPackageUp} from 'read-pkg-up';
console.log(await readPackageUp());
// {
// packageJson: {
// name: 'awesome-package',
// version: '1.0.0',
// …
// },
// path: '/Users/sindresorhus/dev/awesome-package/package.json'
// }
``
*/
export function readPackageUp(options?: NormalizeOptions): Promise<NormalizedReadResult | undefined>;
export function readPackageUp(options: Options): Promise<ReadResult | undefined>;
/**
Synchronously read the closest `package.json` file.
@example
``
import {readPackageUpSync} from 'read-pkg-up';
console.log(readPackageUpSync());
// {
// packageJson: {
// name: 'awesome-package',
// version: '1.0.0',
// …
// },
// path: '/Users/sindresorhus/dev/awesome-package/package.json'
// }
``
*/
export function readPackageUpSync(options?: NormalizeOptions): NormalizedReadResult | undefined;
export function readPackageUpSync(options: Options): ReadResult | undefined;
This requires me to do import by the member.
import { readPackageUp } from 'read-pkg-up'
await readPackageUp()
However, an error occurs, because the implementation is different.
As module.exports
is a function itself, I should import like the below snippet.
import readPackageUp from 'read-pkg-up'
await readPackageUp()
This time, a type error occurs, which requires me to // @ts-ignore
.
Would you publish v7.0.2 to fix this?
Thanks a lot.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.