Socket
Socket
Sign inDemoInstall

get-package-type

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-package-type

Determine the `package.json#type` which applies to a location


Version published
Maintainers
1
Weekly downloads
22,589,589
decreased by-1.67%
Install size
6.74 kB

Weekly downloads

Package description

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

Readme

Source

get-package-type NPM Version

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.

// This never looks at `dir1/`, first attempts to load `./package.json`.
const type1 = await getPackageType('dir1/');

// This attempts to load `dir1/package.json`.
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.

FAQs

Last updated on 19 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc