This page is about the import statement defined in the es module specification
The ES6 import is:
that is used to import:
that have been exported from:
As this feature may not be implemented in all browsers natively at this time., you may need a transpilers.
Not to be confound with the import expression in CSS. See CSS - @import rule.
@import url('http://getbootstrap.com/dist/css/bootstrap.css');
import {foo} from 'https://example.com/.../bar.js';
import {foo} from '/foo/bar.js';
import {foo} from './bar.js';
import {foo} from '../bar.js';
import {foo} from 'bar.js';
import {foo} from 'foo/bar.js';
Loader may have configuration files that define aliases.
Example:
import { myExport } from '~alias'
{
"paths": {
"~alias": ["./my-alias.ts"]
}
}
export default defineConfig({
resolve: {
alias: {
'~utils': resolve(__dirname, './test-utils'),
},
}
}
import defaultMember from "module-name";
import defaultMember, { member [ , [...] ] } from "module-name";
import defaultMember, * as name from "module-name";
import * as name from "module-name";
// if the “module-name” module exports a function named foo(), you can write:
name.foo()
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1 , member2 } from "module-name";
import { member1 , member2 as alias2 , [...] } from "module-name";
import "module-name";
See Javascript - Dynamic Import (Module, Json)
// --myModuleFile.js--
function getJSON(url, callback) {
let xhr = new XMLHttpRequest();
xhr.onload = function () {
callback(this.responseText)
};
xhr.open('GET', url, true);
xhr.send();
}
export function getUsefulContents(url, callback) {
getJSON(url, data => callback(JSON.parse(data)));
}
import { getUsefulContents } from 'myModuleFile.js';
getUsefulContents('http://www.example.com', data => {
doSomethingUseful(data);
});
import Member from "ModuleName";
var Member = require("ModuleName").default;
How to resolve the SyntaxError: Cannot use import statement outside a module?