如何在nodejs中导入

詹姆斯·威斯利

每次我尝试导入我的 nodejs 文件时,我都会收到错误消息,无法在模块外导入

我在 StackOverFlow 上尝试了多种解决方案,例如添加

"type":"module"

并将文件扩展名设置为 .mjs

我想从不同的文件导入常量,但我不知道如何

例子

常量.js


export const help1 = 5
export const help2 = 6
export const help3 = 7

后端.js

import {help1,help2,help3} from './constants.js'

我应该如何导入它们?

广告

您可以使用以下代码。Object.freeze不允许对对象属性进行任何更改。

常量.js

module.exports = Object.freeze({
    HELP1: 5,
    HELP2: 6,
    HELP3: 7
});

后端.js

var constants = require('./constants');

console.log(constants.HELP1); // 5

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章