Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export the library as a CommonJS module #9

Closed
NikhilVerma opened this issue Nov 24, 2020 · 9 comments
Closed

Export the library as a CommonJS module #9

NikhilVerma opened this issue Nov 24, 2020 · 9 comments

Comments

@NikhilVerma
Copy link
Contributor

Thanks for this really useful library! However to use it in most applications it needs to be exported as a CommonJS module. Most bundlers do not process the node_modules folder so you get errors like this:

export * from './axios-cached-dns-resolve'
^^^^^^

SyntaxError: Unexpected token 'export'
    at wrapSafe (internal/modules/cjs/loader.js:1053:16)
    at Module._compile (internal/modules/cjs/loader.js:1101:27)
@NikhilVerma
Copy link
Contributor Author

Another solution is to add "type":"module" in the package.json as referenced here - https://nodejs.org/docs/latest-v12.x/api/esm.html#esm_package_json_type_field

@NikhilVerma
Copy link
Contributor Author

Just to be clear I am using this in my Next.js app and I had to use additional plugins as NextJS doesn't support "type":"module" yet. Related discussion here - vercel/next.js#13553

@tcollinsworth
Copy link
Owner

I originally implemented it as CommonJS, but since node 10, I'm done with babel transpiling and have converted all my projects to newer style. Just import esm before this module and it will work. My initial index files just import esm and jump to another module with imports. That way when node 14 comes along, I only have one file to touch to remove esm. With node 14.17, I've read that import is finally going to be supported.

@tcollinsworth
Copy link
Owner

I've added type to the package.json.

@albanm
Copy link

albanm commented Dec 21, 2021

I am using node 16. I get the error "SyntaxError: Cannot use import statement outside a module".

If I set "type": "module" in my package.json I get "require is not defined in ES module scope, you can use import instead".

I don't really care about commonjs vs ecmascript module, but being forced to switch globally for one small dependency doesn't feel right.

Using const { registerInterceptor } = require('esm')(module)('axios-cached-dns-resolve') seems to work though. Maybe it would be useful to add something to the doc ?

@tcollinsworth
Copy link
Owner

tcollinsworth commented Dec 21, 2021

See README.md Requirements

The esm lib is lacking updates and doesn't currently support optional chaining (elvis), so I've switched to esm-wallaby which is a fork of esm that does support optional chaining.

@albanm
Copy link

albanm commented Dec 21, 2021

I did read the README, I just didn't understand "ECMAScript module (esm), not native esm/.mjs with package.json type: module, requires esm"

I suppose in the first part esm refers to the module system and in the second part to the esm node module that helps loading this type of module.

I was simply saying that something a little bit more explicit, probably with an actual code example, would probably help others.

@tcollinsworth
Copy link
Owner

I don't recommend native esm (type: module, or .mjs extension) because it locks modules from manipulation. Instrumentation with newrelic agent and mocking don't work. Using esm or esm-wallaby supports all the ES6 features but doesn't lock the code.

@tcollinsworth
Copy link
Owner

Thanks for the feedback. I need to update my libraries, so I will take your advice and try to make the docs more clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants