How to import moment.js lib in javascript file? - javascript

I downloaded moment.js in folder lib
In js file I trying to load like that:
var moment = require('../lib/moment'); // require
But In the console I see:
Uncaught ReferenceError: require is not defined
<anonymous> file:///Users/pizhev/Projects/web/js/chart.js:2
What is the correct way to import and use ?
When I trying to import like that:
import moment from '../lib/moment';
I see error: Uncaught SyntaxError: import declarations may only appear at top level of a module

Related

Webpack/jQuery: "Uncaught ReferenceError: $ is not defined" when importing other modules

I have a project that I'm using Webpack for, and I am trying to use the autocomplete feature of jQuery-UI. However, whenever I import jQuery-UI into my app.js file using `import 'jquery-ui/ui/widgets/autocomplete', it breaks jQuery and I get the error:
Uncaught ReferenceError: $ is not defined.
The problem was fixed when I changed import 'jquery-ui/ui/widgets/autocomplete into require(jquery-ui/ui/widgets/autocomplete) in my apps.js file (or index.js or whatever your entry file for Webpack is).
Again, I have no idea why this works, but it does, and I can't argue with that.
Thanks to https://stackoverflow.com/a/42465244/10757677 for the inspiration.

Uncaught SyntaxError: Unexpected identifier on face-api import

I'm trying to make a simple demo application to use face-api. Line 2 of my code is throwing this error when I attempt to import the face-api.min.js file.
I've tried reformatting the import but I haven't been able to get it working.
//Importing Face Detection API
import faceapi from "./face-api.min.js";
Uncaught SyntaxError: Unexpected identifier
As the package documentation says, the correct way to import it is
import * as faceapi from 'face-api.js';
EDIT: For usage in Node.js with CommonJS imports you can do:
const faceapi = require('face-api.js');

Import/export in JavaScript gives "Uncaught SyntaxError: Unexpected identifier"

I have very simple question that I couldnt figure out for days.
I have 2 .js files. One is for configuration. I need to call the data from default.js into app.js. But it gives
Uncaught SyntaxError: Unexpected identifier
I checked all online platforms to find a solution, tried all different combinations of export/import but could not find any solution.
//default.js
export default (config = {
apiKey: "enterYourApiKeyFromOpenWeather"
});
//app.js
import config from "./config/default";
I am using vscode and (import config from "./config/default";) line has underlined with underscores with space. I might need to install a package maybe.
If i write:
const config = require("./config/default");
I get this error:
app.js:2 Uncaught ReferenceError: require is not defined
You need to specify .js on your default.js import, since this is vanilla JS and not part of a build process (such as Gulp) or an express.js app per se.
In your app.js
import config from "./config/default.js";
When including your app.js in your script element, as mentioned by jro in the comments:
...did you set type=module on your script element – jro
You need to set type=module on your script AND your module, e.g.:
<script src="app.js" type="module">
<script src="config/default.js" type="module">
If you don't specify type=module you will run into Uncaught SyntaxError: Unexpected identifier again.

Meteor doesn't import external javascript file

I have a javascript file stored in client/template/missionPage.js from where I would like to import a javascript file stored in client/lib/scripts/textRotate.js I have tried all kind of import paths but none of them worked. e.g import txt from './lib/scripts/textRotate.js';
All o them give the same error: Uncaught error cannot find module './lib/scripts/textRotate.js'. Can anybody help?

Importing js library that doesn't have exports

I have a ngDraggable.js library in my node_modules folder which must be included as dependency as follows:
import draggable from 'ngDraggable';
ngDraggable.js doesn't have exports so I used the work around from some SO answer which I cannot find any more. It was suggested to use an index.js file withing the same folder, which would import ngDraggable.js and export it. For some reason this file was lost and I can't remember that 2 lines of code doing this:( I tried this, but it doesn't work at all:
import ngDraggable from './ngDraggable';
export default angular.module('ngDraggable', [ngDraggable]);
Error:
Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module {"default":{"default":{"_invokeQueue":[],"_configBlocks":[],"_runBlocks":[],"requires":[{}],"name":"ngDraggable"}}} due to:
Error: [ng:areq] Argument 'module' is not a function, got Object
http://errors.angularjs.org/1.6.1/ng/areq?p0=module&p1=not%20a%20function%2C%20got%20Object
at http://localhost:3000/index.js:17216:12
Where 'app' is the main module representing the application and "default" must the module that imports ngDraggable and exposes it for main module
I found the copy of missing file. Here's the code snippet
require('./ngDraggable.js');
module.exports = 'ngDraggable';
you can try
import * as draggable from 'ngDraggable';

Categories