Importing RRule from rrule package in TypeScript. Getting SyntaxError: The requested module 'rrule' is expected to be of type CommonJS

Importing RRule from rrule package in TypeScript. Getting SyntaxError: The requested module 'rrule' is expected to be of type CommonJS

更新:尝试在此处创建最小复制:https://github.com/aioobe/node-issue-repro。它显然非常精简,但显示相同的错误消息。

git clone git@github.com:aioobe/node-issue-repro.git
cd node-issue-repro
npm install
npx tsc && node build/index.js

在我的 TypeScript 源代码中,我有以下内容:

import { RRule } from 'rrule';

// ...

const oddDays = new RRule({
    freq: RRule.DAILY,
    bymonthday: [1, 3, 5]
});

当我运行这个时,我得到以下输出:

(node:31434) ExperimentalWarning: The Node.js specifier resolution in ESM is experimental.
file:///home/aioobe/projects/test/server/database/storage.ts:10
import { RRule } from 'rrule';
         ^^^^^
SyntaxError: The requested module 'rrule' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'rrule';
const { RRule } = pkg;
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
    at ModuleJob.run (internal/modules/esm/module_job.js:137:5)
    at Loader.import (internal/modules/esm/loader.js:165:24)
    at Object.loadESM (internal/process/esm_loader.js:68:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! daily-challenge@0.1.0 start-server: `cross-env NODE_ENV=development node --loader ts-node/esm --es-module-specifier-resolution=node server/server.ts`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the daily-challenge@0.1.0 start-server script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/aioobe/.npm/_logs/2020-09-13T15_53_32_162Z-debug.log
npm run start-server exited with code 1
--> Sending SIGTERM to other processes..

在我的 package.json 我有

{
    ...
    "dependencies": {
        ...
        "rrule": "^2.6.6",
        ...
    },
    ...
    "type": "module"
}

(我需要 "type": "module",否则我会 运行 进入 SyntaxError: Cannot use import statement outside a module。)

在我的 tsconfig.json 我有

{
    "compilerOptions": {
        ...
        "allowJs": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "node",
        ...
    },
    ...
}

我注意到在 rrule.js the package.json does not have "type": "module", which according to this page 中意味着它应该被解释为 CommonJS。

老实说,我真的不知道这对我来说意味着什么。我尝试了各种语法变体以将此包包含在我的文件中(包括错误消息中建议的那个)。我 运行 陷入各种错误,但上面发布的语法是自述文件中 example code 中使用的确切语法。

我使用节点版本 14.9.0,npm 版本 6.14.8,打字稿版本 3.7.2 和 ts-node 8.10.2。我很高兴 upgrade/downgrade,但不幸的是,我 运行 遇到了与节点 12 相同的问题。

我以上述方式使用了很多其他模块,没有任何问题。

尝试替换导入语句:

import { RRule } from 'rrule';

与:

import pkg from 'rrule';
const {RRule} = (pkg as any);

这适用于 Node.js v14.11.0。


它实际上是在您收到的错误消息中建议的:
(node:31434) ExperimentalWarning: The Node.js specifier resolution in ESM is experimental.
file:///home/aioobe/projects/test/server/database/storage.ts:10
import { RRule } from 'rrule';
         ^^^^^
SyntaxError: The requested module 'rrule' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'rrule';
const { RRule } = pkg;

使用 commonjs 作为模块代码生成类型修复了这个问题。 需要进行以下更改:

tsconfig.json

{
  "compilerOptions": {
    ...,
    "module": "commonjs",
    ...
}

package.json

{
  ...
  "type": "commonjs",
  ...
}

index.ts

import { RRule } from "rrule";

“rrule”模块似乎声明了一个无效的 index.d.ts

它说 export default RRule; 但默认导出是整个模块。

这很容易变通:

import pkg from "rrule";
const { RRule } = pkg as any;

这相当于:

import * as pkg from "rrule";
const { RRule } = pkg.default as any;

我们需要在这里使用 any 因为 TS 认为这是 typeof RRule,即使你打印它你会看到它实际上是整个模块主体。这可能是上游错误,或者是实验性 ESM 支持的错误。