为什么我在使用 cloudinary 模块时收到“语法错误:无法在模块外使用 import 语句”错误?

问题描述 投票:0回答:2

我正在尝试调用 cloudinary 模块来调整图像大小。这是我的代码:

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

这是我收到的错误:

  (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

谁能告诉我为什么会出现这个特定错误?

javascript node.js npm module cloudinary
2个回答
0
投票

使用commonJs:

const cloudinary = require('cloudinary');

-2
投票

“导入”只能在 Node.JS 文件作为模块运行时使用。转到 package.json,然后添加键“type”和值“module”。

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

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

或者,您可以更改代码,然后不需要编辑“package.json”文件

import something from Something

const something = require('Something')
© www.soinside.com 2019 - 2024. All rights reserved.