0

I am trying to call the cloudinary module to resize an image. This is my code:

import cloudinary from 'cloudinary';
var cl = new cloudinary.Cloudinary({ cloud_name: "username", secure: true });
new CloudinaryImage("pingu.jpg").resize(scale().width(70).height(53));

Here's the error I'm getting:

  (node:29424) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
c:\Users\...\perspective\cloud.js:1
import cloudinary from 'cloudinary';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1018:16)
    at Module._compile (node:internal/modules/cjs/loader:1066:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
    at Module.load (node:internal/modules/cjs/loader:967:32)
    at Function.Module._load (node:internal/modules/cjs/loader:807:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47

[Done] exited with code=1 in 0.183 seconds

Can anyone tell me why I'm getting this specific error?

Cinn
  • 11
  • 1
  • 1
  • 2
  • ??? The error message tells it to you: "_Cannot use import statement outside a module_". There seems to be also instructions how to fix the issue. – Teemu Jun 10 '22 at 07:24
  • It looks like you are trying to use _import statement outside a module_. If you are using node try [this question](https://stackoverflow.com/questions/58384179/syntaxerror-cannot-use-import-statement-outside-a-module). – Jax-p Jun 10 '22 at 07:25
  • @Teemu I'm new to js and not very familiar with the language, also I was running an individual program and do not have a package.json file, therefore I was confused and decided to ask for help. Tried to look for mjs extensions as ell but did not find any – Cinn Jun 10 '22 at 07:39
  • You may refer to the installation and usage guide documentation for [Cloudinary NodeJS integration - here](https://cloudinary.com/documentation/node_integration). – epasos_573 Apr 11 '23 at 02:22

2 Answers2

0

Use commonJs:

const cloudinary = require('cloudinary');
jkalandarov
  • 564
  • 5
  • 15
-2

'Import' can only be used when the Node.JS file is ran as a module. Go to your package.json, and add in a key 'type' and the value 'module'.

{
  "name": "test",
  "version": "1.0.0",
  "dependencies": {
     ...
  }
}

to

{
  "name": "test",
  "version": "1.0.0",
  "dependencies": {
     ...
  },
  "type": "module"
}

Alternatively, you could change your code from and then you don't need to edit your 'package.json' file

import something from Something

to

const something = require('Something')