Skip to content

Error on using a simple example #1111

Closed

Description

@chuacw

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

  1. ianstormtaylor commented on Sep 20, 2022

    @ianstormtaylor
    Owner

    Sorry about that @chuacw, there was an error in the build in the latest release. Can you try with 0.16.2 and see if that solves your problem?

  2. chuacw commented on Sep 20, 2022

    @chuacw
    Author

    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)

  3. ianstormtaylor commented on Sep 20, 2022

    @ianstormtaylor
    Owner

    @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:

    node -v
    

    (Or potentially the ts-node version, not sure how that works though.)

  4. chuacw commented on Sep 21, 2022

    @chuacw
    Author

    Mentioned above: 16.13.1

  5. ianstormtaylor commented on Sep 21, 2022

    @ianstormtaylor
    Owner

    @chuacw does it still happen when using node directly and not ts-node?

  6. chuacw commented on Sep 21, 2022

    @chuacw
    Author

    The issue got resolved.

    Some weird interaction.

    Thank you, Ian.

  7. mcmire commented on Sep 21, 2022

    @mcmire
    Contributor

    @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.

  8. ianstormtaylor commented on Sep 21, 2022

    @ianstormtaylor
    Owner

    @mcmire that would be great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions