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

SyntaxError: Cannot use import statements outside a module #2

Closed
Batman-Avinash opened this issue Sep 30, 2020 · 3 comments
Closed

Comments

@Batman-Avinash
Copy link

Batman-Avinash commented Sep 30, 2020

System Info:


OS: Linux mint 20 x86_64
Kernel: 5.4.0-48-generic
Node version: 12.14.1

Error message


proshop@1.0.0 start /home/avinash/Projects/Traversy media/proshop_mern
node backend/server

/home/avinash/Projects/Traversy media/proshop_mern/backend/server.js:1
import path from 'path'
^^^^^^

SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:891:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! proshop@1.0.0 start: node backend/server
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the proshop@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/avinash/.npm/_logs/2020-09-30T22_20_45_659Z-debug.log

Debug

@shree1999
Copy link

Hello,
Instead of import try using, const packageName = require("package") or
const name = require("./path to some folder")

The reason why import doesn't work is because we don't have babel configured which compiles es6 and other higher version of javascript to the level where all the browsers can understand it.

and Node doesn't come with babel or webpack configuration

Hope it helps

@NguyenDa18
Copy link

As stated, I have a PR that involves running Babel first to solve this issue -thanks! #3

@bradtraversy
Copy link
Owner

Please update your version of Node to latest. and add "type": "module" in the package.json

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

No branches or pull requests

4 participants