admin管理员组

文章数量:1295913

I'm ing back to Node and TypeScript after being away for a few years, and trying to get back up to speed by building a basic CRUD REST server using Node, Express, Mongoose and TypeScript. I've e across a strange error that I can't quite figure out. A lot has changed since I was last in the weeds on stuff like this (e.g. CommonJS vs ES modules, numerous version revisions of each ponent, etc.) so I've got a lot of balls in the air.

I have code that works fine for a while, and then after touching something random, it will stop working and give me:

SyntaxError: The requested module '../utils/LoggerClass' does not provide an export named 'LoggerClass'

(or the same error on any number of other classes/files I have that analogous)

...even though the LoggerClass file clearly does export something named LoggerClass (because it just worked). WebStorm does not show these errors in the editor. And if I delete all the .js files in my source tree (e.g. find ... | xargs rm) and run it again, it will make the error go away for a while. Some file edits will not recreate the problem, but than at some point I will make a change that will bring the problem back, requiring me to clear out all the .js files again.

My package.json includes:

  "scripts": {
    "start": "node src/server.ts"
  },
  "type": "module",
  "module": "esnext",

I know there have been some changes in the past few years regarding CommonJS vs ES modules, and I can't say I understand everything about them, but I seem to be coding using ES modules and using import instead of require, and, like I said earlier, it usually works fine.

I am running node v22.6.0, tsc v5.5.4 and building/running in WebStorm 2024.2 on MacOS.

In terms of WebStorm configurations:

  1. My TypeScript settings include node 22.6.0 as the interpreter, bundled TypeScript (5.4.3), with TypeScript language service, show project errors and show suggestions enabled (but use types from server and "Repile on changes" off).

  2. My node.js settings include node 22.6.0.

  3. In my project Run Configuration, I have node 22.6.0, TypeScript loader is Bundled (tsx), and node parameters --require ts-node/register

As an example, my LoggerClass.ts file starts with:

import { ILogger } from "../monCode/misc/ILogger";

export class LoggerClass implements ILogger {
    public traceOn = true;
    constructor(public moduleName: string, public debugMode: boolean) { }

and my references to it look like:

import { LoggerClass } from "../utils/LoggerClass";

For what it's worth, when WebStorm runs my app, here is the mand line it uses:

/Users/USERNAME/.nvm/versions/node/v22.6.0/bin/node
  --import file:/Applications/WebStorm.app/Contents/plugins/nodeJS/js/ts-file-loader/node_modules/tsx/dist/loader.cjs
  --require ts-node/register
  /Users/USERNAME/WebstormProjects/APP-NAME/src/server.ts

And my tsconfig.json looks like (with loads of mented out lines elided for readablity):

{
  "pilerOptions": {

    /* Projects */
    // …

    /* Language and Environment */
    "target": "es2016",                                  /* Set the JavaScript language version for emitted JavaScript and include patible library declarations. */
    // …

    /* Modules */
    "module": "monjs",                                /* Specify what module code is generated. */
    // …

    /* JavaScript Support */
    // …

    /* Emit */
    // …

    /* Interop Constraints */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type patibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    // …

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
    //…

    /* Completeness */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
    // …
  }
}


Any idea what could be causing this problem?

本文标签: