JavaScript bad syntax while importing Moment.js
I need to import Moment.js so I add first script and then importing:
<script src="~/Content/js/moment.js" type="moment"></script>
<script>
import moment from 'moment';
</script>
I always get error: import declarations may only appear at top level of a module
Thanks for help.
javascript
|
show 2 more comments
I need to import Moment.js so I add first script and then importing:
<script src="~/Content/js/moment.js" type="moment"></script>
<script>
import moment from 'moment';
</script>
I always get error: import declarations may only appear at top level of a module
Thanks for help.
javascript
type="moment"
?!
– Bergi
Nov 15 '18 at 21:54
You can only useimport
syntax in code that is declared to be a module, not in normal scripts.
– Bergi
Nov 15 '18 at 21:55
I am using this import for first time so I don't know how works type with import.
– Peter Valek
Nov 15 '18 at 21:56
And what to do if I want it import in normal scripts?
– Peter Valek
Nov 15 '18 at 21:57
should that betype="module"
- since you're usingimport
– Bravo
Nov 15 '18 at 21:57
|
show 2 more comments
I need to import Moment.js so I add first script and then importing:
<script src="~/Content/js/moment.js" type="moment"></script>
<script>
import moment from 'moment';
</script>
I always get error: import declarations may only appear at top level of a module
Thanks for help.
javascript
I need to import Moment.js so I add first script and then importing:
<script src="~/Content/js/moment.js" type="moment"></script>
<script>
import moment from 'moment';
</script>
I always get error: import declarations may only appear at top level of a module
Thanks for help.
javascript
javascript
asked Nov 15 '18 at 21:52
Peter ValekPeter Valek
317
317
type="moment"
?!
– Bergi
Nov 15 '18 at 21:54
You can only useimport
syntax in code that is declared to be a module, not in normal scripts.
– Bergi
Nov 15 '18 at 21:55
I am using this import for first time so I don't know how works type with import.
– Peter Valek
Nov 15 '18 at 21:56
And what to do if I want it import in normal scripts?
– Peter Valek
Nov 15 '18 at 21:57
should that betype="module"
- since you're usingimport
– Bravo
Nov 15 '18 at 21:57
|
show 2 more comments
type="moment"
?!
– Bergi
Nov 15 '18 at 21:54
You can only useimport
syntax in code that is declared to be a module, not in normal scripts.
– Bergi
Nov 15 '18 at 21:55
I am using this import for first time so I don't know how works type with import.
– Peter Valek
Nov 15 '18 at 21:56
And what to do if I want it import in normal scripts?
– Peter Valek
Nov 15 '18 at 21:57
should that betype="module"
- since you're usingimport
– Bravo
Nov 15 '18 at 21:57
type="moment"
?!– Bergi
Nov 15 '18 at 21:54
type="moment"
?!– Bergi
Nov 15 '18 at 21:54
You can only use
import
syntax in code that is declared to be a module, not in normal scripts.– Bergi
Nov 15 '18 at 21:55
You can only use
import
syntax in code that is declared to be a module, not in normal scripts.– Bergi
Nov 15 '18 at 21:55
I am using this import for first time so I don't know how works type with import.
– Peter Valek
Nov 15 '18 at 21:56
I am using this import for first time so I don't know how works type with import.
– Peter Valek
Nov 15 '18 at 21:56
And what to do if I want it import in normal scripts?
– Peter Valek
Nov 15 '18 at 21:57
And what to do if I want it import in normal scripts?
– Peter Valek
Nov 15 '18 at 21:57
should that be
type="module"
- since you're using import
– Bravo
Nov 15 '18 at 21:57
should that be
type="module"
- since you're using import
– Bravo
Nov 15 '18 at 21:57
|
show 2 more comments
3 Answers
3
active
oldest
votes
You don't need to import lib in the browser environment.
You referred the lib source and after the DOM is loaded the moment object just will be in the global scope.
Here's an example
var now = moment().format('MMM DD h:mm A');
add a comment |
According to Momentjs docs:
<script src="moment.js"></script>
<script>
moment().format();
</script>
add a comment |
Check the docs
This is how to use moment in the browser
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script>
const today = moment()
console.log(`Today's date is ${today.format('DD MM YYYY')}`)
const tomorrow = today.add(1, 'days')
console.log(`Tomorrow is ${tomorrow.format('dddd')}`)
</script>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328402%2fjavascript-bad-syntax-while-importing-moment-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't need to import lib in the browser environment.
You referred the lib source and after the DOM is loaded the moment object just will be in the global scope.
Here's an example
var now = moment().format('MMM DD h:mm A');
add a comment |
You don't need to import lib in the browser environment.
You referred the lib source and after the DOM is loaded the moment object just will be in the global scope.
Here's an example
var now = moment().format('MMM DD h:mm A');
add a comment |
You don't need to import lib in the browser environment.
You referred the lib source and after the DOM is loaded the moment object just will be in the global scope.
Here's an example
var now = moment().format('MMM DD h:mm A');
You don't need to import lib in the browser environment.
You referred the lib source and after the DOM is loaded the moment object just will be in the global scope.
Here's an example
var now = moment().format('MMM DD h:mm A');
edited Nov 15 '18 at 22:08
answered Nov 15 '18 at 21:58
Air JAir J
243
243
add a comment |
add a comment |
According to Momentjs docs:
<script src="moment.js"></script>
<script>
moment().format();
</script>
add a comment |
According to Momentjs docs:
<script src="moment.js"></script>
<script>
moment().format();
</script>
add a comment |
According to Momentjs docs:
<script src="moment.js"></script>
<script>
moment().format();
</script>
According to Momentjs docs:
<script src="moment.js"></script>
<script>
moment().format();
</script>
answered Nov 15 '18 at 21:58
DmitriyDmitriy
611216
611216
add a comment |
add a comment |
Check the docs
This is how to use moment in the browser
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script>
const today = moment()
console.log(`Today's date is ${today.format('DD MM YYYY')}`)
const tomorrow = today.add(1, 'days')
console.log(`Tomorrow is ${tomorrow.format('dddd')}`)
</script>
add a comment |
Check the docs
This is how to use moment in the browser
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script>
const today = moment()
console.log(`Today's date is ${today.format('DD MM YYYY')}`)
const tomorrow = today.add(1, 'days')
console.log(`Tomorrow is ${tomorrow.format('dddd')}`)
</script>
add a comment |
Check the docs
This is how to use moment in the browser
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script>
const today = moment()
console.log(`Today's date is ${today.format('DD MM YYYY')}`)
const tomorrow = today.add(1, 'days')
console.log(`Tomorrow is ${tomorrow.format('dddd')}`)
</script>
Check the docs
This is how to use moment in the browser
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script>
const today = moment()
console.log(`Today's date is ${today.format('DD MM YYYY')}`)
const tomorrow = today.add(1, 'days')
console.log(`Tomorrow is ${tomorrow.format('dddd')}`)
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script>
const today = moment()
console.log(`Today's date is ${today.format('DD MM YYYY')}`)
const tomorrow = today.add(1, 'days')
console.log(`Tomorrow is ${tomorrow.format('dddd')}`)
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script>
const today = moment()
console.log(`Today's date is ${today.format('DD MM YYYY')}`)
const tomorrow = today.add(1, 'days')
console.log(`Tomorrow is ${tomorrow.format('dddd')}`)
</script>
edited Nov 15 '18 at 22:03
answered Nov 15 '18 at 21:56
ksavksav
5,38721432
5,38721432
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328402%2fjavascript-bad-syntax-while-importing-moment-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
type="moment"
?!– Bergi
Nov 15 '18 at 21:54
You can only use
import
syntax in code that is declared to be a module, not in normal scripts.– Bergi
Nov 15 '18 at 21:55
I am using this import for first time so I don't know how works type with import.
– Peter Valek
Nov 15 '18 at 21:56
And what to do if I want it import in normal scripts?
– Peter Valek
Nov 15 '18 at 21:57
should that be
type="module"
- since you're usingimport
– Bravo
Nov 15 '18 at 21:57