swagger-jsdoc icon indicating copy to clipboard operation
swagger-jsdoc copied to clipboard

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:

Open Romick2005 opened this issue 4 years ago • 22 comments

After installing v.7.0.0-rc.2 cannot start the project, because of invalid ES module import used. image

Romick2005 avatar Feb 19 '21 08:02 Romick2005

@Romick2005 which version of node.js are you using? Have you checked https://github.com/Surnet/swagger-jsdoc#nodejs-version-requirements-commonjs-and-esm? Did you check the suggestion made by the error handler in the message you shared?

kalinchernev avatar Feb 19 '21 09:02 kalinchernev

node v12.18.1 npm 6.14.5 I do not have "type": "module" in my package.json

Romick2005 avatar Feb 19 '21 09:02 Romick2005

index.js in v7 uses import instead of require. v7: image v6: image

Romick2005 avatar Feb 19 '21 09:02 Romick2005

It's the library that becomes a module in 7.x. It'd be useful for consumers who need ESM and I don't think it's your case. In your case, you are trying to use the library with require, which tells node.js to resolve modules as CommonJS.

You can use 6.x which has module.exports, yes.

kalinchernev avatar Feb 19 '21 09:02 kalinchernev

It's unfortunately quite messy the cjs vs esm and it has been in the last couple of years. See this, I hope it'll be useful as a general knowledge https://nodejs.org/api/esm.html A solution from the library would be to use a transpilation step before publishing so to provide both esm and cjs. I don't have the time for another rewriting at the moment though. It's frustrating for me as a library maintainer and for the consumers, but again: you don't need to bump to v7 if you don't need ESM

kalinchernev avatar Feb 19 '21 09:02 kalinchernev

I think it is bettter to go with require and import map in package.json for version 7 to support both CommonJS and ES Modules. Like this:

"exports": {
    "require": "./index.js",
    "import": "./esm/wrapper.js"
}

where wrapper.js looks like:

import cjsModule from '../index.js';
export const foo = cjsModule.foo;

Note that it’s easy to write an ESM wrapper for CJS libraries, but it’s not possible to write a CJS wrapper for ESM libraries.

Romick2005 avatar Feb 19 '21 13:02 Romick2005

If you can make an example, please open a pull request, I welcome ideas

kalinchernev avatar Feb 19 '21 13:02 kalinchernev

I notice an increased activity (likes :) on comments here, but i don't know whether this means there is a better way or people come and see a solution of their problem.

Any suggestions how to handle this better than the 7.x rewrite or shall I close?

kalinchernev avatar Mar 05 '21 14:03 kalinchernev

@kalinchernev I just downgrade swagger-jsdoc to 6.x. Otherwise it looks like I need to add { "type": "module" } into my package.json and use import/export everywhere in my project.

The first solution is purrfectly fine with me.

KoteKotlyarov avatar Mar 05 '21 14:03 KoteKotlyarov

@kalinchernev I don't think we should use ESM modules as it's really new in the Node.js ecosystem. If we use ESM, we should also provide compatibility with CommonJS.

Also there's another issue, the v7 version is released under the latest tag, so when I run npm install swagger-jsdoc, it automatically install v7 but instead it should install v6, since the v7 is a 'rc' (release candidate).

theoludwig avatar Mar 14 '21 09:03 theoludwig

Hi @Divlo thank you for your feedback. Unfortunately, I feel that the weeks I spent making ESM version of the library are causing more confusion than benefit although my attempts to provide the 2 options natively with documentation and tests, suffering from the drawbacks of being on the leading edge.

For the moment, I keep this issue open, reply to issues, etc. but I already feel tired with the idea of yet another rewrite or a bundler setup which will stray the project off the main goal of being native node.js

As a "workaround", please use version 6 for CJS and 7 for ESM

kalinchernev avatar Mar 14 '21 12:03 kalinchernev

Just spent hours trying to figure out what was wrong because the rc is released via the latest tag. Please fix before releasing v7 and please put v6 on the latest tag.

binaryben avatar Mar 19 '21 07:03 binaryben

I mostly agree that we should gradually migrate CommonJS modules to ESM. I really think it is the right think to do, I really wish, CommonJS didn't existed at all or that the whole ecosystem already migrated.

Currently if I upgrade to latest version of this library, my tests fails, that's why I stated that it is too early, in fact it is not, and I will update when others dependencies will be ready!

Thanks for making this happen! @kalinchernev

theoludwig avatar Apr 22 '21 23:04 theoludwig

@kalinchernev I just downgrade swagger-jsdoc to 6.x. Otherwise it looks like I need to add { "type": "module" } into my package.json and use import/export everywhere in my project.

The first solution is purrfectly fine with me.

its not working for me

a-dams154 avatar May 07 '21 17:05 a-dams154

Any solution for this..?

a-dams154 avatar May 07 '21 17:05 a-dams154

@kalinchernev I just downgrade swagger-jsdoc to 6.x. Otherwise it looks like I need to add { "type": "module" } into my package.json and use import/export everywhere in my project. The first solution is purrfectly fine with me.

its not working for me

now its working for me downgrading swagger js-doc to 6.0.1

a-dams154 avatar May 08 '21 07:05 a-dams154

I was facing this issue, downgrading to 6.0.7 worked for me

premacck avatar May 29 '21 13:05 premacck

Just spent hours trying to figure out what was wrong because the rc is released via the latest tag. Please fix before releasing v7 and please put v6 on the latest tag.

Gotta agree here. v7 really shouldn't be marked as the latest version on npm since it is still pre-release.

I'm all for the move to ESM, and @kalinchernev you should feel confident it's definitely the right move going forward. The confusion comes from the fact that ESM is new, and running npm i swagger-jsdoc will install 7.0.0-rc.6 as of writing.

~For those getting this issue, run the following command and it will install the latest CJS (^6.x.x) version.~

~npm install swagger-jsdoc@6~

Edit: NPM now installs the latest version as per @daniloab's comment below so just uninstall and reinstall:

npm remove swagger-jsdoc
npm install swagger-jsdoc

lachieh avatar May 30 '21 00:05 lachieh

the npm latest version was fixed for the latest one: 6.1.0 as @lachieh said.

We advise swagger-jsdoc users to use it since is stabilized and running perfectly.

You can check here https://www.npmjs.com/package/swagger-jsdoc

daniloab avatar Jun 01 '21 12:06 daniloab

@a-dams154 bug from #270 using the release candidate version, it is very similar. Let's add here to have a robust track about it:

error code : 'ERR_REQUIRE_ESM' image

daniloab avatar Jun 01 '21 12:06 daniloab

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 31 '21 13:07 stale[bot]