swagger-jsdoc
swagger-jsdoc copied to clipboard
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:
After installing v.7.0.0-rc.2 cannot start the project, because of invalid ES module import used.
@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?
node v12.18.1 npm 6.14.5 I do not have "type": "module" in my package.json
index.js in v7 uses import instead of require.
v7:
v6:
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.
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
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.
If you can make an example, please open a pull request, I welcome ideas
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 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.
@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).
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
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.
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
@kalinchernev I just downgrade
swagger-jsdoc
to 6.x. Otherwise it looks like I need to add{ "type": "module" }
into mypackage.json
and useimport/export
everywhere in my project.The first solution is purrfectly fine with me.
its not working for me
Any solution for this..?
@kalinchernev I just downgrade
swagger-jsdoc
to 6.x. Otherwise it looks like I need to add{ "type": "module" }
into mypackage.json
and useimport/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
I was facing this issue, downgrading to 6.0.7 worked for me
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
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
@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'
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.