简体   繁体   English

导入在 Node.js 版本 11.8.0 上不起作用

[英]Import not working on Node.js version 11.8.0

I am writing a simple program that uses a object full of dictionary words.我正在编写一个简单的程序,它使用一个充满字典单词的对象。 I want to import that object from a different file as it is very large.我想从另一个文件导入该对象,因为它非常大。 When trying to import it I get an error that looks like Node.js doesn't know what it is.尝试导入它时,我收到一个错误,看起来 Node.js 不知道它是什么。

I have already tried reinstalling the latest version of Node.js.我已经尝试重新安装最新版本的 Node.js。

Here is the important code:这是重要的代码:

import {dict} from './words_dictionary'

And here is all of it:这就是全部:

import {dict} from './words_dictionary'


function exists(obj,str) {
  if(obj[str]) {
    return true
  } else {
    return false
  }
}
console.log(exists(dict, 'hello'))

Here is the gist of the dictionary code:这是字典代码的要点:

export let dict = {a: 1, aa: 1, aaa: 1, aah: 1, aahed: 1, aahing: 1, aahs:
1, aal: 1, aalii: 1, aaliis: 1, aals: 1, aam: 1, aani: 1, aardvark: 1,
aardvarks: 1,...~3000 more}

I expected true, but I got this error:我期望是真的,但我收到了这个错误:

SyntaxError: Unexpected token {
at new Script (vm.js:84:7)
at createScript (vm.js:264:10)
at Object.runInThisContext (vm.js:312:10)
at Module._compile (internal/modules/cjs/loader.js:696:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at tryModuleLoad (internal/modules/cjs/loader.js:568:12)
at Function.Module._load (internal/modules/cjs/loader.js:560:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
at executeUserCode (internal/bootstrap/node.js:526:15)

ECMAScript 6 is now working, but I am now getting the error of dict not being defined. ECMAScript 6 现在正在运行,但我现在收到未定义 dict 的错误。 Could this have something to do with the file size, because I have checked multiple times for spelling errors?这是否与文件大小有关,因为我已经多次检查拼写错误?

Have you been able to use the import keyboard elsewhere in your code?您是否能够在代码的其他地方使用import键盘? The issue here may be that you aren't transpiling your code into ECMAScript 5. Since import is an ECMAScript 6 feature, it hasn't yet been fully supported by Node.js.这里的问题可能是您没有将代码转换为 ECMAScript 5。由于import是 ECMAScript 6 的一项功能,Node.js 尚未完全支持它。 If you use a tool like Babel to transpile your code, you may fix this issue.如果你使用像 Babel 这样的工具来转译你的代码,你可以解决这个问题。 If you don't want to do this, try using require instead.如果您不想这样做,请尝试改用require

As noted, in Node.js 9+ you can also use it in .mjs files with the --experimental-modules flag enabled.如前所述,在 Node.js 9+ 中,您还可以在启用了--experimental-modules标志的 .mjs 文件中使用它。

node --experimental-modules file.mjs

Node.js import compatibility Node.js import兼容性

It's only supported with an experimental flag.它仅受实验性标志支持。 You should use the --experimental-modules flag.您应该使用--experimental-modules标志。

Or just use require simple as that or if you really want, you can transpile your code with browserify, babel or parcel or whatever.或者只是使用 require simple 或者如果你真的想要,你可以使用 browserify、babel 或 parcel 或其他任何东西来转换你的代码。

I think this should work if you run code like this:我认为如果您运行这样的代码,这应该可以工作:

node --experimental-modules index.mjs

Note that it uses the mjs extension (modular JavaScript I think).请注意,它使用mjs扩展(我认为是模块化 JavaScript)。

You try this.你试试这个。 Hope it helps希望能帮助到你

const 'your_variable' = require('your_required_module or file_path')

In your case在你的情况下

const  dict = require( './words_dictionary')
Install necessary packages

npm install @babel/core @babel/register @babel/preset-env --save-dev

Add start.js file
// Transpile all code following this line with babel and use 
'@babel/preset-env' (aka ES6) preset.

require("@babel/register")({
  presets: ["@babel/preset-env"]
});

// Import the rest of our application.
module.exports = require('./server.js')

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

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