Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using import in any .js file #25792

Closed
AlexiRJamal opened this issue Jan 29, 2019 · 11 comments
Closed

When using import in any .js file #25792

AlexiRJamal opened this issue Jan 29, 2019 · 11 comments
Labels
question Issues that look for answers.

Comments

@AlexiRJamal
Copy link

Node version: v8.11.1

When using const something = require('lib) everything works fine. But when using import something from 'lib' it shows this:

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

I don't know what's wrong, or if I'm doing anything wrong :/
Thanks anyways for any help :)
@AlexiRJamal
Copy link
Author

Basically, when using any ES6 syntax. I tried using 'use strict' but it didn't work.

@ChALkeR ChALkeR added the question Issues that look for answers. label Jan 29, 2019
@Hakerh400
Copy link
Contributor

Make sure that

  • Node.js is started using --experimental-modules flag
  • Script extension is .mjs

Example: node --experimental-modules script.mjs

@himself65
Copy link
Member

himself65 commented Jan 29, 2019

another way to use import/export is that install @babel/core and @babel/node

https://babeljs.io/docs/en/babel-node

after adding to your dependencies or devDependencies (I support to second) then run babel-node index.js

@AlexiRJamal
Copy link
Author

Make sure that

  • Node.js is started using --experimental-modules flag
  • Script extension is .mjs

Example: node --experimental-modules script.mjs

That was part of the problem. My extensions were .js only.

Thanks man.

@AlexiRJamal
Copy link
Author

another way to use import/export is that install @babel/core and @babel/node

https://babeljs.io/docs/en/babel-node

after adding to your dependencies or devDependencies (I support to second) then run babel-node index.js

I have babel code installed as global. Dunno why it raised this error in the first place. Ironically when working with React it doesn't happen at all :/

@gamboa1980
Copy link

gamboa1980 commented Jan 31, 2019 via email

@himself65
Copy link
Member

himself65 commented Feb 1, 2019

another way to use import/export is that install @babel/core and @babel/node
https://babeljs.io/docs/en/babel-node
after adding to your dependencies or devDependencies (I support to second) then run babel-node index.js

I have babel code installed as global. Dunno why it raised this error in the first place. Ironically when working with React it doesn't happen at all :/

react using webpack with other babel parts to transfer code, babel-node just the lib that is easy to run in time

I think you used it in the wrong way

@AlexiRJamal
Copy link
Author

another way to use import/export is that install @babel/core and @babel/node
https://babeljs.io/docs/en/babel-node
after adding to your dependencies or devDependencies (I support to second) then run babel-node index.js

I have babel code installed as global. Dunno why it raised this error in the first place. Ironically when working with React it doesn't happen at all :/

react using webpack with other babel parts to transfer code, babel-node just the lib that is easy to run in time

I think you used it in the wrong way

Do I need to require the babel-node too or...?

@fosteman
Copy link

Hey @loox37
Was the issue solved?

@GrosSacASac
Copy link
Contributor

Having babel installed will not make Node use babel automatically. You need to be explicit.
babel-node should not be required inside the code. Instead It is a drop in replacement for the node command. So instead of
node index.js
do
npx @babel/node index.js
assuming it is in devDependencies

@AlexiRJamal
Copy link
Author

Thanks for all of you.
The issue was resolved by using babel, I forgot to close this question.
I'm not one of these guys who say "solved, thank you" without leaving the answer:
Install babel
If necessary, do npm link
Then use import.

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

7 participants