最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Import not working on Node.js version 11.8.0 - Stack Overflow

programmeradmin3浏览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.

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?

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?

Share Improve this question edited Aug 30, 2019 at 16:22 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Jan 27, 2019 at 0:28 MilesZewMilesZew 6871 gold badge8 silver badges21 bronze badges 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). – paulsm4 Commented Jan 27, 2019 at 0:41
Add a comment  | 

5 Answers 5

Reset to default 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

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论