Error on using a simple example
See original GitHub issueHi, 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:
- Created a year ago
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ianstormtaylor If Node.js >= 16 is required to use this library what do you think about adding an
engines
property topackage.json
? We just encountered the “Unexpected token” error on Node 14. Happy to submit a PR.@mcmire that would be great, thank you!