2

I am running WebStorm 2024.4.3.5 and created a Node.js project, using Fastify, React, and TypeScript. I can run my server.ts file using the Edit Configuration Node.js menu item. The "Typescript loader" is specified to be "Bundled (tsx)" and I am using node 23.11.0 through nvm.

My server runs perfectly when WebStorm executes the following:

~/.nvm/versions/node/v23.11.0/bin/node --import file:/snap/webstorm/451/plugins/nodeJS/js/ts-file-loader/node_modules/tsx/dist/loader.cjs ~/WebstormProjects/untitled6/src/server/server.ts

Looks like the magic is this special tsx file loader.cjs.

If I just try node src/server/server.ts I get all kinds of errors:

  • Cannot find modules for imports in which I do not explicitly specify the .ts extension in the import statement.
  • Cannot use fastify-autoload unless I write the to be loaded code in js, instead of ts.
  • And, this error:
    import fastify, { FastifyInstance, FastifyServerOptions } from "fastify";
                      ^^^^^^^^^^^^^^^
    SyntaxError: Named export 'FastifyInstance' not found.
    

It is all very frustrating chasing this around. I would like to run it the way it is run in WebStorm outside of WebStorm.

Some errors suggest that I use ts-node, but I have heard ts-node is being deprecated, and I have tried that as well, to no avail.

I guess my question here is:

Where does one get this magical file loader?


EDIT: some people asked for package.config and tsconfig.json, which I added below.

Another asked: Why are you trying to --import a cjs file. I am not. That is the command that WebStorm issues to start my server, which makes it run. WebStorm seems to have its own specialty loader.cjs from the WebStorm code base.

package.config

{
  "name": "untitled6",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "proxy": "http://localhost:9090",
  "scripts": {
    "dev": "vite",
    "build": "tsc -b && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@fastify/autoload": "^6.3.0",
    "@fastify/cors": "^11.0.1",
    "@fastify/static": "^8.1.1",
    "@types/leaflet": "^1.9.17",
    "fastify": "^5.2.2",
    "fastify-autoload": "^3.12.0",
    "leaflet": "^1.9.4",
    "nano-cache": "^1.1.2",
    "node-persist": "^4.0.4",
    "react": "^19.1.0",
    "react-dom": "^19.1.0",
    "react-leaflet": "^5.0.0",
    "react-router": "^7.5.0"
  },
  "devDependencies": {
    "@fastify/vite": "^8.0.0",
    "@types/nano-cache": "^1.1.4",
    "@types/node": "^22.14.0",
    "@types/node-persist": "^3.1.8",
    "@types/react-dom": "^19.1.1",
    "@types/react-helmet": "^6.1.11",
    "@vitejs/plugin-react": "^4.3.4",
    "json-schema-to-ts": "^3.1.1",
    "react-helmet": "^6.1.0",
    "typescript": "~5.7.2",
    "vite": "^6.2.0"
  }
}

tsconfig.node.json

{
  "compilerOptions": {
    "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
    "target": "ES2022",
    "lib": ["ES2023"],
    "module": "ESNext",
    "skipLibCheck": true,
    "verbatimModuleSyntax": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "esModuleInterop": true,
    "allowImportingTsExtensions": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmit": true,

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedSideEffectImports": true
  },
  "include": ["vite.config.ts"]
}
3
  • node 24 can execute .ts files directly. No need for ts-node. Why are you trying to --import a .cjs file? Commented Apr 11 at 0:58
  • Would you like to share the package.json and tsconfig.json file? That would give us more to point you in the right direction. Commented Apr 11 at 6:07
  • I added them. Awaiting edit approval. Commented Apr 12 at 17:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.