Import not working on Node.js version 11.8.0
Asked Answered
L

5

5

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.

I have already tried reinstalling the latest version of 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. Could this have something to do with the file size, because I have checked multiple times for spelling errors?

Lundquist answered 27/1, 2019 at 0:28 Comment(1)
Q1: Has this ever worked, with any version of NodeJS? Q2: Does the file pass JSON validation (for example, https://jsonlint.com)? If Q1= N and Q2= Y, consider substituting "require" for "import" (and changing your syntax accordingly).Congregation
T
8

Have you been able to use the import keyboard elsewhere in your code? 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. If you use a tool like Babel to transpile your code, you may fix this issue. If you don't want to do this, try using require instead.

As noted, in Node.js 9+ you can also use it in .mjs files with the --experimental-modules flag enabled.

node --experimental-modules file.mjs

Node.js import compatibility

Tessera answered 27/1, 2019 at 0:37 Comment(0)
S
3

It's only supported with an experimental flag. You should use the --experimental-modules flag.

Or just use require simple as that or if you really want, you can transpile your code with browserify, babel or parcel or whatever.

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).

Stallworth answered 27/1, 2019 at 0:37 Comment(0)
M
1

You try this. Hope it helps

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

In your case

const  dict = require( './words_dictionary')
Macruran answered 9/12, 2019 at 2:5 Comment(0)
C
1
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')
Consanguineous answered 20/7, 2021 at 3:8 Comment(0)
B
1

This type of Error can be caused by different kinds of mistake.
Make sure that while using import statement, your file type must be module.
This can be achieved by using '.mjs' extension OR
by using "type": "module" in package.json
And for module file use '.cjs' extension

Use this to get rid of most of the errors-

import * as name from "module_path"

OR import {function/variable} from "path"

If you are using VS Code then
U may install "Flow Language Support" for ease in work.

Benzidine answered 9/5, 2023 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.