bradtraversy / proshop_mern

Shopping cart built with MERN & Redux
1.99k stars 1.18k forks source link

SyntaxError: Cannot use import statements outside a module #2

Closed Batman-Avinash closed 3 years ago

Batman-Avinash commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

As stated, I have a PR that involves running Babel first to solve this issue -thanks! https://github.com/bradtraversy/proshop_mern/pull/3

bradtraversy commented 3 years ago

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