0

Problem

I use replit to practice code online while creating files to refer to each object in my programming study. But whenever I opened a different file and intended to run it in console (not shell), it can only run the default file that I've already deleted or renamed, instead of the file that I currently opened. It has the same behaviour as if we renamed the file, but it's much more easier to fix th problem in that case. However, I started researching and trying to figured out how to solve this problem. First, by installing the glob library. And then I create a new file in the current directory I've been working to referring it using some code provided by chatGPT. And then I configured the .json file to change its "main" (like you can see in the screenshots) and also the .replit file to change the entrypoint etc. One time it works, but there's still a bit issue that all the files run at the same time in the console. Then I try to figure out how to make the console only run one file at a time separately, but it looks like there are too many options that make me confuse. Anyone can help me solve this?

Solution

  1. Install glob library in shell using npm install glob command
  2. Create new file named runner.js to configure the glob library
  3. Update the .json file by change the "main" to loops.js (current file.js)
  4. Configure .replit file and change its entrypoint and all refferences to runner.js

Result

  1. The library has been installed succesfully
  2. Inside runner.js:
const glob = require('glob');
const path = require('path');

// Get an array of all JavaScript files in the directory
const files = glob.sync('*.js', { ignore: ['runner.js'] });

// Require and run each file
files.forEach(file => {
  if (file !== 'runner.js') {
    const filePath = `./${file}`;
    const absolutePath = path.resolve(filePath);
    require(absolutePath);
  }
});
  1. Updated .json file by change its "main" to the current file.js:
{
  "name": "nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "loops.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^18.0.6",
    "glob": "^10.3.3",
    "node-fetch": "^3.2.6"
  }
}
  1. Inside .replit :
entrypoint = "runner.js"

hidden = [".config", "package-lock.json"]

[interpreter]
command = [
    "prybar-nodejs",
    "-q",
    "--ps1",
    "\u0001\u001b[33m\u0002\u0001\u001b[00m\u0002 ",
    "-i"
]

[[hints]]
regex = "Error \\[ERR_REQUIRE_ESM\\]"
message = "We see that you are using require(...) inside your code. We currently do not support this syntax. Please use 'import' instead when using external modules. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)"

[nix]
channel = "stable-22_11"

[env]
XDG_CONFIG_HOME = "$REPL_HOME/.config"
PATH = "$REPL_HOME/.config/npm/node_global/bin:$REPL_HOME/node_modules/.bin"
npm_config_prefix = "$REPL_HOME/.config/npm/node_global"

[gitHubImport]
requiredFiles = [".replit", "replit.nix", ".config", "package.json", "package-lock.json"]

[packager]
language = "nodejs"

  [packager.features]
  packageSearch = true
  guessImports = true
  enabledForHosting = false

[unitTest]
language = "nodejs"

[debugger]
support = true

  [debugger.interactive]
  transport = "localhost:0"
  startCommand = [ "dap-node" ]

    [debugger.interactive.initializeMessage]
    command = "initialize"
    type = "request"

      [debugger.interactive.initializeMessage.arguments]
      clientID = "replit"
      clientName = "replit.com"
      columnsStartAt1 = true
      linesStartAt1 = true
      locale = "en-us"
      pathFormat = "path"
      supportsInvalidatedEvent = true
      supportsProgressReporting = true
      supportsRunInTerminalRequest = true
      supportsVariablePaging = true
      supportsVariableType = true

    [debugger.interactive.launchMessage]
    command = "launch"
    type = "request"
    
      [debugger.interactive.launchMessage.arguments]  
      args = []
      console = "externalTerminal"
      cwd = "."
      environment = []
      pauseForSourceMap = false
      program = "./runner.js"
      request = "launch"
      sourceMaps = true
      stopOnEntry = false
      type = "pwa-node"

[languages]

[languages.javascript]
pattern = "**/{*.js,*.jsx,*.ts,*.tsx,*.json}"

[languages.javascript.languageServer]
start = "typescript-language-server --stdio"

[deployment]
run = ["sh", "-c", "node runner.js"]

Details

Files and outputs Update for .replit #1 Update for .replit #2

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Aug 19 '23 at 16:53

0 Answers0