Ask coding questions

← Back to all posts
import not working in node
ruiwenge2

When I use import, it gives an error. Does anyone know what's wrong and how I can fix it?

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
ruiwenge2
Comments
hotnewtop
Coder100

it's not in a cjs file

anyways, import requires some configuration, so in your package.json (maybe use npm init -y to add?) add:

oh not to mention you never even exported the functions silly

ruiwenge2

@Coder100 i tried that but it gave the same error

Coder100
ruiwenge2

@Coder100 so you need a bash command?

Coder100

until replit supports node 14 either use nix bash or that @ruiwenge2

ruiwenge2

@Coder100 Oh ok

heyitsmarcus

I see you have the require syntax in your code, but is there a particular reason that you feel you need to use import syntax?

I made a fork of your repl that uses the standard export syntax and changed the filename to ".js" instead of ".cjs" so that you can do an implicit require on the file, as well. The only extra line it needs is the module.exports at the bottom that puts the hi and bye functions as exportable functions.

https://replit.com/@heyitsmarcus/import#functions.js