0

I have an index.js file that I want to load in the Node REPL to try some stuff, but when I use .load index.js in the REPL, it goes in an infinite loop and keeps repeating the first line in the file const mongoose = require('mongoose');. I found an alternative solution which works in Ubuntu 20.04.5 in WSL2, which is to use the command node -i -e "$(< index.js)" in the terminal which loads the file perfectly fine and I can interact with its contents. But when I try the same command in PowerShell it gives me this error:

< : The term '<' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:1 char:15
+ node -i -e "$(< index.js)"
+               ~
    + CategoryInfo          : ObjectNotFound: (<:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

C:\Program Files\nodejs\node.exe: -e requires an argument

The reason I'm asking about PowerShell "even though I use Ubuntu and things work there", is that I'm taking a web development course, and I provided the solution of using node -i -e "$(< index.js)" to people who were having the same issue, but other people can't get this to work in PowerShell, so I'm just trying to help. and I couldn't find any solution online to this .load issue, or to using the node -i -e "$(< index.js)" command in PowerShell.

index.js contents:

const mongoose = require('mongoose');
mongoose.set('strictQuery', false);
mongoose.connect('mongodb://localhost:27017/movieApp', { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => {
        console.log("CONNECTION OPEN!!!")
    })
    .catch(err => {
        console.log("OH NO ERROR!!!!")
        console.log(err)
    })

const movieSchema = new mongoose.Schema({
  title: String,
  year: Number,
  score: Number,
  rating: String
});

const Movie = mongoose.model('Movie', movieSchema);

const amadeus = new Movie({
  title: 'Amadeus',
  year: 1986,
  score: 9.2,
  rating: 'R'
});
pers0n
  • 135
  • 8
  • Does this answer your question? [Problem loading a JS script into the Node REPL](https://stackoverflow.com/questions/75121070/problem-loading-a-js-script-into-the-node-repl) – starball Mar 18 '23 at 18:49

2 Answers2

0

In my experience $(...) on PowerShell acts strangely, and doesn't produce the expected result every time. Also < operator is not currently supported on Windows.

However, I managed to get the desired behaviour by using:

node -i -e echo "./index.js"
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • Thanks for your reply. It runs the file, but I can't interact with it as I would in the REPL when using `.load index.js` – pers0n Jan 29 '23 at 00:36
  • Yes i checked now and it seems like the -i argument is ignored. I did try multiple ways of passing the file/path to see if that's the problem but so far it doesn't make a difference, node seems to be completely ignoring the -i argument. I'll have to look more into it. – f1ght3r Jan 30 '23 at 12:05
0

I am taking the same class. I haven't been able to get the .load index.js to work in PowerShell either -- even after updating node to the current version (v19) (rather than the LTS (v18)). But the command node -i -e "$(< index.js)" does seem to work properly in a gitBash terminal (which apparently is what the course recommends, at least according to some posts from the TAs). But the command given in the lecture doesn't seem to work in any terminal shell.