question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error on using a simple example

See original GitHub issue

Hi, I’m using superstruct in a simple project, and got the following error when trying to run it with nodejs.

Error [ERR_REQUIRE_ESM]: require() of ES Module Development\teststruct\node_modules\superstruct\lib\index.cjs.js from Development\teststruct\teststruct.ts not supported.
index.cjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.cjs.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in Development\teststruct\node_modules\superstruct\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

package.json

{
  "name": "teststruct",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "superstruct": "^0.16.1"
  },

  "devDependencies": {
    "ts-node": "^10.9.1",
    "typescript": "^4.8.3"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es2016",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "module": "commonjs",                                /* Specify what module code is generated. */
    "moduleResolution": "node",                       /* Specify how TypeScript looks up a file from a given module specifier. */

    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    // "preserveSymlinks": true,                         /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}

teststruct.ts

import { create, object, number, string, defaulted } from 'superstruct';

let i = 0

const User = object({
  id: defaulted(number(), () => i++),
  name: string(),
})

const data = {
  name: 'Jane',
}

// You can apply the defaults to your data while validating.
const user = create(data, User)
console.log(user);

I’m using node v16.13.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mcmirecommented, Sep 21, 2022

@ianstormtaylor If Node.js >= 16 is required to use this library what do you think about adding an engines property to package.json? We just encountered the “Unexpected token” error on Node 14. Happy to submit a PR.

0reactions
ianstormtaylorcommented, Sep 21, 2022

@mcmire that would be great, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Errors and exceptions — Object-Oriented Programming in ...
Syntax errors are mistakes in the use of the Python language, and are analogous to ... Here are some examples of mistakes which...
Read more >
Incorrect program example: Using the error code parameter
The error code parameter provides a way for you to determine whether an API encounters any errors. Here are the program examples that...
Read more >
On Error Statement - Visual Basic | Microsoft Learn
This example first uses the On Error GoTo statement to specify the location of an error-handling routine within a procedure. In the example,...
Read more >
Types of Errors in Java with Examples - GeeksforGeeks
Types of Errors in Java with Examples · Error is an illegal operation performed by the user which results in the abnormal working...
Read more >
Error Calculation: Meaning, Types & Examples - StudySmarter
These formulae are simple to remember, and you should use them both sequentially to complete thorough error analysis of your completed experiment. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found