NLCST to String Converter
nlcst-to-string is a utility designed to serialize NLCST (Natural Language Concrete Syntax Tree) nodes into their plain-text string representation. It is part of the unified ecosystem, focusing specifically on text content extraction from linguistic ASTs. The current stable version is 4.0.0. Releases follow the unified collective's compatibility policy, typically dropping support for unmaintained Node.js versions with new major releases (e.g., v4 requires Node.js 16+). Key differentiators include its tight integration with the NLCST specification, its minimal API (a single `toString` function), and its robust handling of various NLCST node types by prioritizing plain-text fields or recursively serializing children. This package is an ESM-only module since version 3.0.0 and ships with full TypeScript type definitions.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` to import `nlcst-to-string` which is an ESM-only package since v3.0.0.fixChange `const toString = require('nlcst-to-string')` to `import { toString } from 'nlcst-to-string'`. -
TypeError: toString is not a function
cause This can occur if an incorrect import path is used, or if a build process incorrectly bundles the ESM module, or if an older version of Node.js (prior to 12.17.0 for full ESM support) is used with a CommonJS context.fixVerify that `import { toString } from 'nlcst-to-string'` is correctly configured for an ESM environment and that Node.js 16+ is used for `nlcst-to-string@4`. -
SyntaxError: Cannot use import statement outside a module
cause You are trying to use ES module `import` syntax in a CommonJS context (e.g., a `.js` file without `"type": "module"` in `package.json`).fixEnsure your file is treated as an ES module by either renaming it to `.mjs` or adding `"type": "module"` to your project's `package.json`. Alternatively, for older Node.js versions or CJS-only environments, you might need to use an older version of `nlcst-to-string` (pre-v3) or a dynamic `import()` call.
Warnings
- breaking Version 4.0.0 changed to require Node.js 16 or higher. Projects running on older Node.js versions will encounter compatibility issues.
- breaking Version 4.0.0 removed the `separator` option. Although noted as likely unused, any code relying on this option will break.
- breaking Version 3.0.0 converted the package to be ESM-only. CommonJS `require()` statements will no longer work, leading to module loading errors.
- gotcha The package uses the `exports` field in `package.json` (since v4.0.0). Direct access to internal paths or non-exported files may break.
Install
-
npm install nlcst-to-string -
yarn add nlcst-to-string -
pnpm add nlcst-to-string
Imports
- toString
const toString = require('nlcst-to-string')import { toString } from 'nlcst-to-string' - toString (browser)
import { toString } from 'nlcst-to-string'import { toString } from 'https://esm.sh/nlcst-to-string@4?bundle' - Type definitions
import { Node, toString } from 'nlcst-to-string'import type { Node } from 'nlcst' import { toString } from 'nlcst-to-string'
Quickstart
import { toString } from 'nlcst-to-string';
import type { Parent, Text } from 'nlcst';
const exampleNode: Parent = {
type: 'SentenceNode',
children: [
{ type: 'WordNode', children: [{ type: 'TextNode', value: 'Hello' }] },
{ type: 'PunctuationNode', value: ',' },
{ type: 'SpaceNode', value: ' ' },
{ type: 'WordNode', children: [{ type: 'TextNode', value: 'world' }] },
{ type: 'PunctuationNode', value: '!' }
]
};
const complexWordNode: Parent = {
type: 'WordNode',
children: [
{ type: 'TextNode', value: 'AT' },
{ type: 'SymbolNode', value: '&' },
{ type: 'TextNode', value: 'T' }
]
};
console.log(toString(exampleNode));
console.log(toString(complexWordNode));
// Expected output:
// Hello, world!
// AT&T