Sign inDemoInstall


Package Overview
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies



parse SPDX license expressions

Version published
Install size
27.8 kB

Package description

What is spdx-expression-parse?

The spdx-expression-parse npm package is designed to parse SPDX license expressions. SPDX (Software Package Data Exchange) is a standard format for communicating the licensing information of software packages. This package helps in parsing and validating the syntax of SPDX expressions, making it easier to handle complex licensing scenarios programmatically.

What are spdx-expression-parse's main functionalities?

Parsing SPDX expressions

This feature allows the parsing of SPDX license expressions into a structured format. The code sample demonstrates how to parse a simple SPDX expression that includes an OR operator, indicating dual licensing options.

const parse = require('spdx-expression-parse');
const expression = 'MIT OR Apache-2.0';
const parsedExpression = parse(expression);

Handling complex SPDX expressions

This feature supports the parsing of more complex SPDX expressions, including nested groups and exceptions. The code sample shows how to parse an expression that combines multiple licenses with logical AND and an exception clause.

const parse = require('spdx-expression-parse');
const complexExpression = '(MIT OR Apache-2.0) AND (GPL-2.0+ WITH Classpath-exception-2.0)';
const parsedComplexExpression = parse(complexExpression);

Other packages similar to spdx-expression-parse



This package parses SPDX license expression strings describing license terms, like package.json license strings, into consistently structured ECMAScript objects. The npm command-line interface depends on this package, as do many automatic license-audit tools.

In a nutshell:

var parse = require('spdx-expression-parse')
var assert = require('assert')

  // Licensed under the terms of the Two-Clause BSD License.
  {license: 'BSD-2-Clause'}

assert.throws(function () {
  // An invalid SPDX license expression.
  // Should be `Apache-2.0`.
  parse('Apache 2')

  // Dual licensed under either:
  // - LGPL 2.1
  // - a combination of Three-Clause BSD and MIT
  parse('(LGPL-2.1 OR BSD-3-Clause AND MIT)'),
    left: {license: 'LGPL-2.1'},
    conjunction: 'or',
    right: {
      left: {license: 'BSD-3-Clause'},
      conjunction: 'and',
      right: {license: 'MIT'}

The syntax comes from the Software Package Data eXchange (SPDX), a standard from the Linux Foundation for shareable data about software package license terms. SPDX aims to make sharing and auditing license data easy, especially for users of open-source software.

The bulk of the SPDX standard describes syntax and semantics of XML metadata files. This package implements two lightweight, plain-text components of that larger standard:

  1. The license list, a mapping from specific string identifiers, like Apache-2.0, to standard form license texts and bolt-on license exceptions. The spdx-license-ids and spdx-exceptions packages implement the license list. spdx-expression-parse depends on and require()s them.

    Any license identifier from the license list is a valid license expression:

    var identifiers = []
      .filter(function (id) { return id[id.length - 1] !== '+' })
    identifiers.forEach(function (id) {
      assert.deepEqual(parse(id), {license: id})

    So is any license identifier WITH a standardized license exception:

    identifiers.forEach(function (id) {
      require('spdx-exceptions').forEach(function (e) {
          parse(id + ' WITH ' + e),
          {license: id, exception: e}
  2. The license expression language, for describing simple and complex license terms, like MIT for MIT-licensed and (GPL-2.0 OR Apache-2.0) for dual-licensing under GPL 2.0 and Apache 2.0. spdx-expression-parse itself implements license expression language, exporting a parser.

      // Licensed under a combination of:
      // - the MIT License AND
      // - a combination of:
      //   - LGPL 2.1 (or a later version) AND
      //   - Three-Clause BSD
      parse('(MIT AND (LGPL-2.1+ AND BSD-3-Clause))'),
        left: {license: 'MIT'},
        conjunction: 'and',
        right: {
          left: {license: 'LGPL-2.1', plus: true},
          conjunction: 'and',
          right: {license: 'BSD-3-Clause'}

This package differs slightly from the SPDX standard in allowing lower- and mixed-case AND, OR, and WITH operators:

  parse('MIT or BSD-2-Clause'),
  { left: { license: 'MIT' }, conjunction: 'or', right: { license: 'BSD-2-Clause' } }
  parse('GPL-2.0 with GCC-exception-2.0'),
  { license: 'GPL-2.0', exception: 'GCC-exception-2.0' }

The Linux Foundation and its contributors license the SPDX standard under the terms of the Creative Commons Attribution License 3.0 Unported (SPDX: "CC-BY-3.0"). "SPDX" is a United States federally registered trademark of the Linux Foundation. The authors of this package license their work under the terms of the MIT License.



Last updated on 21 Nov 2023

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.


Related posts

SocketSocket SOC 2 Logo


  • 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