What is get-package-type?
The `get-package-type` npm package is designed to determine the type of a Node.js package. It can identify if a package is a CommonJS or an ECMAScript module by analyzing the package's configuration and structure. This is particularly useful in environments where understanding the module type is necessary for compatibility or transformation purposes.
What are get-package-type's main functionalities?
Determine package type
This feature allows you to determine whether a given package is a CommonJS or ECMAScript module. The function `getPackageType` takes the path to the package as an argument and returns a promise that resolves with the type of the package.
const getPackageType = require('get-package-type');
async function checkPackageType() {
const type = await getPackageType('/path/to/package');
console.log(`Package type: ${type}`); // 'commonjs' or 'module'
}
checkPackageType();
Other packages similar to get-package-type
read-pkg
The `read-pkg` package reads the `package.json` file of a Node.js project and parses it into a JavaScript object. While it doesn't directly determine the module type like `get-package-type`, it can be used to manually check the `type` field in the `package.json` to infer if a package is a CommonJS or ECMAScript module.
pkg-dir
The `pkg-dir` package finds the root directory of a Node.js project or npm package. This can be a complementary tool to `get-package-type` by first finding the root directory of a package and then using `get-package-type` to determine the package type. However, `pkg-dir` itself does not provide functionality to identify the module type.
get-package-type
Determine the package.json#type
which applies to a location.
Usage
const getPackageType = require('get-package-type');
(async () => {
console.log(await getPackageType('file.js'));
console.log(getPackageType.sync('file.js'));
})();
This function does not validate the value found in package.json#type
. Any truthy value
found will be returned. Non-truthy values will be reported as commonjs
.
The argument must be a filename.
const type1 = await getPackageType('dir1/');
const type2 = await getPackageType('dir1/index.cjs');
The extension of the filename does not effect the result. The primary use case for this
module is to determine if myapp.config.js
should be loaded with require
or import
.