trekhleb / javascript-algorithms

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
MIT License
185.13k stars 29.86k forks source link

run playground.js or any other .js file #926

Closed ccknaus closed 1 year ago

ccknaus commented 1 year ago

I am also new to JS, and I very much suffer playing around with the repository. Although all tests suffice, how I am suppose to run playground.js or any other created .js file? I also have difficulties importing the classes. I am not certain wheter the modules are written in ES6 or CommonJS?

Whenever I run playground.js in VSC tells me: (node:9600) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

However, when I change package.json by adding a line "type": "module", I get another error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module ...

trekhleb commented 1 year ago

@ccknaus you may find some helpful details in the How to use this repo section of README.

The idea is that instead of launching the ***.js script directly, you write the test ***.test.js script that checks what your ***.js script should do. And then you may just launch: npm test -- 'playground'

Take a look at the playground example:

Feel free to update these files in your local environment. Imports should work, console.log() should work. So you'll be able to experiment with algorithms.

ccknaus commented 1 year ago

@trekhleb tank you for your response. I went through the fibonacci example.

Like i mentioned, I am fairly new to JS and git as well, so I am wondering why I can't import modules at all. Probabily I am at fault here, so I have to dig deeper into package manager stuff and such.