PS D:\vscode\tutorial\JavaScript_qwzn\examples> node .\09.01.range.js internal/process/esm_loader.js:74 ? ? internalBinding('errors').triggerUncaughtException( ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'D:\vscode\tutorial\JavaScript_qwzn\examples\06.01.inherit' imported from D:\vscode\tutorial\JavaScript_qwzn\examples\09.01.range.js Did you mean to import ../06.01.inherit.js? ? ? at finalizeResolution (internal/modules/esm/resolve.js:271:11) ? ? at moduleResolve (internal/modules/esm/resolve.js:694:10) ? ? at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:805:11) ? ? at Loader.resolve (internal/modules/esm/loader.js:88:40) ? ? at Loader.getModuleJob (internal/modules/esm/loader.js:241:28) ? ? at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:72:40) ? code: 'ERR_MODULE_NOT_FOUND' }
出现这个错误,很奇怪。。
import??{inherit}?from?'./06.01.inherit';
导入包的时候,缺少.js
环境:
npm init
npm install babel-loader
运行:node? range.js
range.js
// range.js: A class representing a range of values.
import {inherit} from './06.01.inherit.js';
// This is a factory function that returns a new range object.
function range(from, to) {
// Use the inherit() function to create an object that inherits from the
// prototype object defined below. The prototype object is stored as
// a property of this function, and defines the shared methods (behavior)
// for all range objects.
var r = inherit(range.methods);
// Store the start and end points (state) of this new range object.
// These are noninherited properties that are unique to this object.
r.from = from;
r.to = to;
// Finally return the new object
return r;
}
// This prototype object defines methods inherited by all range objects.
range.methods = {
// Return true if x is in the range, false otherwise
// This method works for textual and Date ranges as well as numeric.
includes: function(x) { return this.from <= x && x <= this.to; },
// Invoke f once for each integer in the range.
// This method works only for numeric ranges.
foreach: function(f) {
for(var x = Math.ceil(this.from); x <= this.to; x++) f(x);
},
// Return a string representation of the range
toString: function() { return "(" + this.from + "..." + this.to + ")"; }
};
// Here are example uses of a range object.
var r = range(1,3); // Create a range object
r.includes(2); // => true: 2 is in the range
r.foreach(console.log); // Prints 1 2 3
console.log(r); // Prints (1...3)
inherit.js
// inherit() returns a newly created object that inherits properties from the
// prototype object p. It uses the ECMAScript 5 function Object.create() if
// it is defined, and otherwise falls back to an older technique.
function inherit(p) {
if (p == null) throw TypeError(); // p must be a non-null object
if (Object.create) // If Object.create() is defined...
return Object.create(p); // then just use it.
var t = typeof p; // Otherwise do some more type checking
if (t !== "object" && t !== "function") throw TypeError();
function f() {}; // Define a dummy constructor function.
f.prototype = p; // Set its prototype property to p.
return new f(); // Use f() to create an "heir" of p.
}
export {inherit}
|