GithubHelp home page GithubHelp logo

Comments (9)

albanm avatar albanm commented on August 18, 2024 1

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 ?

from axios-cached-dns-resolve.

NikhilVerma avatar NikhilVerma commented on August 18, 2024

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

from axios-cached-dns-resolve.

NikhilVerma avatar NikhilVerma commented on August 18, 2024

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

from axios-cached-dns-resolve.

tcollinsworth avatar tcollinsworth commented on August 18, 2024

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.

from axios-cached-dns-resolve.

tcollinsworth avatar tcollinsworth commented on August 18, 2024

I've added type to the package.json.

from axios-cached-dns-resolve.

tcollinsworth avatar tcollinsworth commented on August 18, 2024

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.

from axios-cached-dns-resolve.

albanm avatar albanm commented on August 18, 2024

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.

from axios-cached-dns-resolve.

tcollinsworth avatar tcollinsworth commented on August 18, 2024

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.

from axios-cached-dns-resolve.

tcollinsworth avatar tcollinsworth commented on August 18, 2024

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

from axios-cached-dns-resolve.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.