Require NOT import!

it seems that import is being used all over the place and throwing errors as a result. i.e. in index.js in the root of the project. import cannot be used yet with node.js! What is going on here? In fact, this type of error is being thrown wherever there is import, export default, etc.

1 Like

I’m getting this problem also… app crashing due to this :frowning:

Yes. It’s crazy. And I’m on Node 11.9 and npm 6.5.

import keyword, export default, all throw errors. Node.js still is not accepting that syntax I don’t think. I’m seeing babel 6+, but we should be using babel 7+ by now. I tried to change according to the workflow I use, but more issues creep up.

In addition, I am able to connect via the shell. I already use compass for MongoDB projects in another account, but in this one, because my admin user is what is being populated into the Compass connection string and not the user for this course, I might be having problems with my .env configuration (as well). I did all the steps in the README. My data is populated in Compass as well. It’s the code that is causing me trouble. What is going on here!!! I know Jest is most of the problem, so I don’t know how it is affecting everything else!

@interglobalmediad did you run npm install and did everything install correctly?

The use of import is fine since we’re using babel.

Yeah, I am running on Glitch.com - it automatically npm installs everything. I develop a lot of NodeJS stuff there, but this crashes due to the import statements.

Glitch may not be able to support the required features @codemzy

Worked fine on Glitch for the MFlix and Chill application (on previous course). I will try and spin it up in another dev environment.

@codemzy yes we did that one a bit different (it was a spin off of an early version of mflix).

Could you try adding this to your package.json?

"babel": {
  "presets": [
    "env",
    "stage-1"
  ],
  "plugins": [
    "transform-runtime"
  ]
}
1 Like

yes. but then i got all these es6 related syntax errors like i described and a lot of jest specific errors. I was able to restore the data to Compass too! And Node is still not accepting import export default etc. So why is that being used in some places? And testing is not configured consistently. Jest has require, but mocha (I take it describe is done mocha) contains import syntax which should not be there either. I could go on and on here.

I will go back to the original version and try and add this. We’ll see what happens. I have to take the environment/set up on my machine into consideration too.

That did the trick for me in Glitch! Thanks :+1:

1 Like

Glad it worked! I’ll make sure the babel information gets added to the distributed package.json file for folks in the future who want to use Glitch.

@interglobalmedia The difference in usage between require and import among the files is necessary due to entry points for babel and associated plugins to transpile the code into ES2015. It was a conscious choice to use very modern syntax and features, and the resounding feedback we’ve heard is that people are enjoying it.

Based on the errors you are mentioning it really seems like you had a bad installation, didn’t copy all hidden files over, or are running in a non-standard environment. Could you provide a screenshot of your mflix-js directory with hidden files shown?

I downloaded again but kept the data folder. Then I npm i, and got no vulnerabilities or npm errors. Then I ran npm test -t db-connection and got the following in the console:

npm test -t db-connection                                                                      ✭

> server@1.0.0 test /Users/mariacam/Development/mflix-js
> jest --passWithNoTests "db-connection"

Determining test suites to run...Setup Mongo Connection
FAIL  test/db-connection.test.js
● Test suite failed to run

/Users/mariacam/Development/mflix-js/test/db-connection.test.js:1
({"Object. 
<anonymous>":function(module,exports,require,__dirname,__filename,global,jest) 
{import MoviesDAO from "../src/dao/moviesDAO";
                                                                                                ^^^^^^^^^

SyntaxError: Unexpected identifier

at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.905s, estimated 6s
Ran all test suites matching /db-connection/i.
Teardown Mongo Connection
npm ERR! Test failed.  See above for more details.

The data folder isn’t necessary after you’ve restored the data into Atlas, so if you don’t want to keep it you certainly don’t have to.

I just followed the steps you described and it worked on my end. I’m currently researching this and should have an answer soon.

Attached is the screenshot.

Thanks for that! Your env file should be named .env. Also, do you have a .eslintrc, .prettierrc, and .babelrc file in your handouts?

Edited to add, you’ll also want to add a .gitignore file if you are using git. Here is the contents of one we use

# dependencies
/node_modules

# testing
/coverage

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
1 Like

Yes. It should. But you wanted to be able to see it!

I use a .gitignore file. I will add that content. I did not receive ANY config files for prettier, for eslint, babel, nothing. Thought it very strange.

Edit: I found the files. I forgot to do ls -a. They are now copied over to the project directory. Thanks!

Strange indeed. I just downloaded the handout and it was included in mine.

No worries. I’ve put the files in a gist for you to copy/paste.

1 Like

Don’t know where the other thread is, but everything is working fine. Perhaps the hidden files can be added to the tree in the video lecture? That we are alerted that they are there. I had copied my files and folders into a new directory and forgot to do an ls -a. Thanks for the heads up! Appreciate it.

1 Like