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 Filesnodejsnode.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'
});
2
Answers
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:
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 commandnode -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.