Repository navigation
Closed
Labels
Description
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
Activity
ianstormtaylor commentedon Sep 20, 2022
Sorry about that @chuacw, there was an error in the build in the latest release. Can you try with
0.16.2and see if that solves your problem?chuacw commentedon Sep 20, 2022
I got this error after installing 0.16.2 and compiling the app.
\node_modules\superstruct\lib\index.cjs:40
return cached ??= [failure, ...failures()];
^^^
SyntaxError: Unexpected token '??='
at wrapSafe (internal/modules/cjs/loader.js:984:16)
at Module._compile (internal/modules/cjs/loader.js:1032:27)
at Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Object.require.extensions. [as .js] (K:\Development\TypeScript\teststruct\node_modules\ts-node\src\index.ts:1608:43)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object. (K:\Development\TypeScript\teststruct\teststruct.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:1068:30)
ianstormtaylor commentedon Sep 20, 2022
@chuacw I believe that's due to an older Node.js version. (I think the defaults compile to the latest LTS version of Node, which right now is
16.) Do you know what version you're running? You can check with:(Or potentially the
ts-nodeversion, not sure how that works though.)chuacw commentedon Sep 21, 2022
Mentioned above: 16.13.1
ianstormtaylor commentedon Sep 21, 2022
@chuacw does it still happen when using
nodedirectly and notts-node?chuacw commentedon Sep 21, 2022
The issue got resolved.
Some weird interaction.
Thank you, Ian.
mcmire commentedon Sep 21, 2022
@ianstormtaylor If Node.js >= 16 is required to use this library what do you think about adding an
enginesproperty topackage.json? We just encountered the "Unexpected token" error on Node 14. Happy to submit a PR.ianstormtaylor commentedon Sep 21, 2022
@mcmire that would be great, thank you!