There is still nothing built-in to provide the exact functionality you describe. However, an alternative to using require it to use the .load command within the REPL, like such:
.load foo.js
It loads the file in line by line just as if you had typed it in the REPL. Unlike require this pollutes the REPL history with the commands you loaded. However, it has the advantage of being repeatable because it is not cached like require.
Which is better for you will depend on your use case.
Edit: It has limited applicability because it does not work in strict mode, but three years later I have learned that if your script does not have 'use strict', you can use eval to load your script without polluting the REPL history:
var fs = require('fs');
eval(fs.readFileSync('foo.js').toString())
Answer from vossad01 on Stack OverflowThere is still nothing built-in to provide the exact functionality you describe. However, an alternative to using require it to use the .load command within the REPL, like such:
.load foo.js
It loads the file in line by line just as if you had typed it in the REPL. Unlike require this pollutes the REPL history with the commands you loaded. However, it has the advantage of being repeatable because it is not cached like require.
Which is better for you will depend on your use case.
Edit: It has limited applicability because it does not work in strict mode, but three years later I have learned that if your script does not have 'use strict', you can use eval to load your script without polluting the REPL history:
var fs = require('fs');
eval(fs.readFileSync('foo.js').toString())
i always use this command
node -i -e "$(< yourScript.js)"
works exactly as in Python without any packages.
It is possible in Node.js v14, but you need to use the import operator, rather than the import declaration.
$ node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> let myModule;
undefined
> import("./my-module.js").then(module => { myModule = module });
Promise { <pending> }
> myModule.foo();
"bar"
This is not currently possible. ES modules are supposed to be imported from the ES module scope, while REPL isn't considered one. This can improve with time because the support of ES modules is experimental. The use of require and import is mutually exclusive in the Node.js module implementation, and REPL already uses require.
Dynamic import is supported in the REPL since Node.js 13. With node --experimental-repl-await, it is:
await import('./right.mjs');
» npm install @opentf/react-node-repl
» npm install local-repl
I'm afraid that the answer is no, or at least not yet. We know that it will be supported in the future, but the ES2015 import and export won't act as in-place substitutions to CommonJS require and module.exports. This is simply because they don't work the same way. You may find more information in this Node.js blog post and this SO question. In particular, one can already run and import standard modules in Node.js, under the flag --experimental-modules (relevant 2ality blog post). However, this feature will still not work from the REPL.
Your idea of using babel-node (which is now called babel-cli) was not that bad: babel implements an interoperable require function, which is kind of a shim that can retrieve modules designed for either type system. Unfortunately, the official website claims lack of support for this functionality in the REPL:
ES6-style module-loading may not function as expected
Due to technical limitations ES6-style module-loading is not fully supported in a babel-node REPL.
Nonetheless, this issue does not apply to production code, which should not rely on the REPL anyway. Instead of using the REPL, you may consider writing a script in a bare-bones project containing babel-cli, and create a script for running the transpiled version:
{
"name": "my-test-chamber",
"private": true,
"scripts": {
"build": "babel src/main.js -o lib/main.js",
"start": "npm run build && node lib/main.js"
},
"dev-dependencies": {
"babel-cli": "^6.0.0"
}
}
I found a way to import the ES6 Modules into REPL (I don't remember where):
PS> node
Welcome to Node.js v18.17.0.
Type ".help" for more information.
> const {platform, type} = await import("node:os");
undefined
> console.log('Platform:',platform(), 'Type:', type());
Platform: win32 Type: Windows_NT
undefined
>
> const path = await import("node:path");
undefined
> path.sep
'\\'
You cannot use static import statements (e.g. import someModule from "some-module"), at the moment. I'm not aware of any efforts/tickets/pull requests/intents to change that.
You can use import() syntax to load modules! This returns a promise. So for example you can create a variable someModule, start importing, & after done importing, set someModule to that module:
let someModule
import("some-module")
.then( loaded=> someModule= loaded)
Or you can directly use the import in your promise handler:
import("some-module").then(someModule => someModule.default())
For more complex examples, you might want to use an async Immediately Invoked Function Expression, so you can use await syntax:
(async function(){
// since we are in an async function we can use 'await' here:
let someModule = await import("some-module")
console.log(someModule.default())
})()
last, if you start Node.JS with the --experimental-repl-await flag, you can use async directly from the repl & drop the async immediately invoked function:
let someModule = await import("some-module")
// because you have already 'await'ed
// you may immediately use someModule,
// whereas previously was not "loaded"
console.log(someModule.default())
Given the static nature of import statements and that the engine needs to know all static imports (before processing any non-import related code). You can see how the import statement really isn't compatible with interactive REPL support.
For example, import statements are allowed in the middle or end of a file, but they're hoisted and processed before "non-static-import related code".
What should the REPL do if you type a static import statement at the end of your REPL session?
It can't easily go back and rerun all your prior commands in light of this potentially fundamental change!
I didn't think about that until reading this. Thanks Kevin Qian who also has ideas.