首页 > 解决方案 > 从命令行使用 Node 导入运行 Javascript

问题描述

我对从命令行运行 javascript 有点不熟悉,但我处于需要为松下 AC 使用 NPM 包的情况,该包有一个用于其非官方 API 的包装器。

但是,我想制作一个可以从命令行运行的简单 JS,它将导入 NPM 包。但这只会产生错误:

(node:68628) 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)
/Users/Ronny/Documents/AC/index.js:1
import { ComfortCloudClient } from 'panasonic-comfort-cloud-client'
^^^^^^

SyntaxError: Cannot use import statement outside a module

难道不能这样做吗?

标签: javascriptnode.jsnpm

解决方案


它可以使用节点require()功能

const { ComfortCloudClient } = require('panasonic-comfort-cloud-client')

推荐阅读