Can't use an import statement outside a module?

This page summarizes the projects mentioned and recommended in the original post on /r/typescript

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • ts-node

    TypeScript execution and REPL for node.js

  • By default, Node assumes you're targeting CJS. This error is a node error telling you that "import ..." is a syntax error when you're configured for running CJS. Based on your description, I suspect you have "module": "es2015" or higher in your tsconfig. This tells typescript to output files that are compatible with ESM. At this point, if you change this to "module": "commonjs", you'd likely have a working set of code (this is my recommended approach). But... you took the experimental long route 😀. By adding "type": "module", you told node to run it as if it were ESM. Now you have Node and TS targeting ESM, but ts-node doesn't have an easy way of being compatible with ESM. Switching the loader to use ts-node/esm is then what put all 3 tools into the same ecosystem configuration. Note that this config is a bit fragile, the hooks are experimental, so Node doesn't follow semver with them. Minor version changes in node may break compatibility. Which leads into...

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts