【问题标题】:nodejs import module is not working as expecting,nodejs 导入模块没有按预期工作,
【发布时间】:2020-07-24 14:43:17
【问题描述】:

我有以下结构。

node_modules
src
 - app.js
 - newrelic.js
package.json
package-lock.json

在 newrelic.js 中它是一个简单的类

const axios = require('axios')

export default class Newrelic {
  static getName() {
   return 'Hello;
  }
}

在 app.js 中

import Newrelic from "./newrelic";
console.log(Newrelic.getName())

当我运行 node src/app.js 时,我收到了

import Newrelic from "./newrelic";
^^^^^^^
SyntaxError: Cannot use import statement outside a module

我需要什么来完成这项工作?

【问题讨论】:

    标签: node.js babeljs package.json


    【解决方案1】:

    package.json 中添加字段"type": "module",您的代码将起作用。

    更多信息在这里: https://nodejs.org/api/esm.html

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 2020-05-05
      相关资源
      最近更新 更多