Friday, 18 September 2020

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

Update: Tried to create a minimal repro here: https://github.com/aioobe/node-issue-repro. It's very stripped down obviously, but it exhibits the same error message.

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

In my TypeScript source code I have the following:

import { RRule } from 'rrule';

// ...

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

When I run this, I get the following output:

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

In my package.json I have

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

(I need "type": "module", otherwise I'm running into SyntaxError: Cannot use import statement outside a module.)

In my tsconfig.json I have

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

I've noted that in rrule.js the package.json does not have "type": "module", which according to this page means that it should be interpreted as CommonJS.

Honestly, I don't really know what this means for me. I've tried all kinds of syntax variations to include this package in my file (including the one suggested in the error message). I run into various errors, but the syntax posted above is the exact syntax used in the example code in the readme.

I use node version 14.9.0, npm version 6.14.8, typescript version 3.7.2 and ts-node 8.10.2. I'm happy to upgrade/downgrade, but I'm running into the same issue with node 12 unfortunately.

I use lots of other modules the above way without any problem.



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

No comments:

Post a Comment