1

I maintain a node.js library and I tried to add the following piece of code:

const curlirize = require('axios-curlirize')

And I get the following error (on node version 19.x): SyntaxError: Cannot use import statement outside a module. I saw this answer on SO and I tried adding "type": "module" in my package.json and that did not work either.

Although master build is passing, PR build is failing. What am I doing wrong? I looked at another library with the same package structure as me that uses require('axios-curlirize') and that library works just fine!

PS: I actually do not want type: module since I need it to be CJS - but I want to understand why other code works with the require works but not mine!

pathikrit
  • 32,469
  • 37
  • 142
  • 221
  • 2.0.0 - Uses ES Native Modules 1.3.7 - Uses CommonJS Modules . after looking at the github – cmgchess Mar 06 '23 at 08:45
  • also with type:module you need to use import syntax – cmgchess Mar 06 '23 at 08:47
  • @cmgchess: I don't think that's the issue. I tried using 1.3.7 here: https://github.com/pathikrit/node-visionect/pull/2/commits/70033ac40c602a51a3b0b4fbcdab9934610fb1ec Same error. What am I doing wrong? – pathikrit Mar 06 '23 at 08:47
  • try using 2.0.0 with type:module and import syntax. or 13.7 and require syntax and remove type:module from package json – cmgchess Mar 06 '23 at 08:49
  • > also with type:module you need to use import syntax – Ok so how do I fix this? I tried 1.3.7 and/or removing the `type:module` - I still get same build error. When I see other usages in Github, it uses the `require` style: https://github.com/search?q=curlirize+language%3AJavaScript&type=code&l=JavaScript – pathikrit Mar 06 '23 at 08:51
  • As I said, I cannot use the import syntax. The `require` syntax does not work with 1.3.7 with or without the `type:module`. You can see each failure by clicking on the red checks here: https://github.com/pathikrit/node-visionect/pull/2 – pathikrit Mar 06 '23 at 08:54

1 Answers1

1

Looks like axios-curlirize versions v1.4.0 (deprecated) and v2.0.0 are using ESM.

If you want to use CJS, you would need to use version v1.3.7 or lower instead, but looking at the errors you've posted, they are being caused by installing the wrong version of the package.

The version set on package.json is using caret (i.e. ^1.3.7) which would mean your package manager (npm, yarn, etc.) is trying to install the latest available minor version: v1.4.0 - which is using ESM and is something you probably don't want to use in this case.

To fix this, you can use tilde (i.e. ~1.3.7) to avoid updating to future minor releases and lock the version to accept patch releases only (v1.3.x):

  1. In the curlirize branch, update the package.json and set axios-curlirize to ~1.3.7.

    "axios-curlirize": "~1.3.7"
    
  2. Run yarn install to update yarn.lock and the dependencies.

  3. Run yarn test and axios-curlirize related issues should be resolved.

For more info about package.json dependency versions, please see: What's the difference between tilde(~) and caret(^) in package.json?

Arnesfield
  • 478
  • 4
  • 6
  • Yup! That was it - I pored over every character that might differ between my code and sources I found on Github and I missed the `~` vs `^` .... – pathikrit Mar 10 '23 at 16:05
  • 1
    OTOH, this is a bad usage of SEMVER by axios-curlirize -> Going from CJS to ESM is a major breaking change and they should not have used a minor version increment to do that! – pathikrit Mar 10 '23 at 16:07
  • 1
    I agree. Though, looking at their releases from GitHub, they don't have a release for `v1.4.0`. I'm guessing `v1.4.0` was an accident / mistake and they couldn't do anything but deprecated it since they weren't able to unpublish it from npm. – Arnesfield Mar 11 '23 at 02:46