简体   繁体   English

NodeJS 导入 NPM 模块

[英]NodeJS import NPM module

I have a.js file that I am trying run through node.我有一个我正在尝试通过节点运行的 .js 文件。 The problem that I am having is that my file requires an import of i18next.我遇到的问题是我的文件需要导入 i18next。 Node of course does not like this. Node当然不喜欢这样。

I have already added "type":"module to my package.json . Inside my file accessLayer.js I am trying to import i18next as:我已经在我的accessLayer.js中添加了"type":"module 。在我的文件package.json中,我试图将 i18next 导入为:

import i18next from 'i18next'; const {languageTranslate} = i18next;

For my code inside my terminal I am running:对于我的终端内的代码,我正在运行:

>node --experimental-repl-await
>var aa
>import("./app/accessLayer.js").then(loaded => aa = loaded)

if I console.log() aa it returns [Module: null prototype] { } .如果我console.log() aa 它返回[Module: null prototype] { }

Inside my file is a modules.export which has an authenticate that I am trying to access.我的文件里面是一个modules.export ,它有一个我试图访问的身份验证。

Now, if I completely remove the i18next import from accessLayer.js .现在,如果我从accessLayer.js完全删除 i18next 导入。 I can then run:然后我可以运行:

>node
>var x
>var y
>var x = require('./app/accessLayer.js')
> var y = x()
> y.authenticate('xxxx')

This way does work, however, then I do not have access to i18next, which is needed.这种方式确实有效,但是,我无法访问所需的 i18next。 Any advice or direction is greatly appreciated.非常感谢任何建议或方向。 My version of node is: 14.17.6.我的节点版本是:14.17.6。

Try using尝试使用

import * as i18next from 'i18next';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM