Can't import module from ES 6 module which requires - javascript

My module has type set as module in package.json, hence it is ES 6.
I need to import a module (which I do using import), but this module (dexie-encrypted) does require to a third module, therefor failing:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/xxx/xxx/node_modules/dexie/dist/modern/dexie.mjs
at new NodeError (node:internal/errors:329:5)
at Module.load (node:internal/modules/cjs/loader:970:11)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (/Users/xxx/xxx/node_modules/dexie-encrypted/dist/applyMiddleware.js:5:41)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14) {
code: 'ERR_REQUIRE_ESM'
}
How is a situation like this solved?

Related

Using function from module in JavaScript script

I want to use an exported constant from a JS module. My simple script is like
#!/usr/bin/env node
const pi = require('./custom_math').pi
console.log(pi)
But the module I require has an import, so It raises the following error:
$ ./script.js
<path_to_module>/index.js:1
import anotherconstant from '<other_module>'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (./script.js:3:12)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
How can I make a script that uses the pi constant from the simple_math/index.js file?

Can I use package.json "resolutions" to substitute in lodash-es for lodash?

Pretty straightforward, I'm wondering if resolutions can be used in this fashion (substituting in a different) package like you can for dependencies.
"resolutions": {
"lodash": "npm:lodash-es#^14.7.x"
},
And if I can do that with NPM or Yarn, are there problems beyond the normal risks if I were to force lodash to a certain version.
Maybe it can work, but I couldn't make it work with Webpack's DLL generation.
C:\projects\myProject\node_modules\lodash\lodash.js:10
export { default as add } from './add.js';
^^^^^^
SyntaxError: Unexpected token 'export'
at Module._compile (internal/modules/cjs/loader.js:892:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:\projects\myProject\node_modules\concurrently\src\flow-control\kill-others.js:1:11)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
error Command failed with exit code 1.

How do I configure Laravel 5.3 with Vue 2?

Every time I gulp watch, I get this
module.js:327
throw err;
^
Error: Cannot find module 'laravel-elixir-vue'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/ubuntu/workspace/gulpfile.js:3:1)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
How do I configure Laravel 5.3 with Vue in a logical order?
I've already pulled in Vue with npm install vue
Tell me if I forgot something crucial to add to this question.

Having problems while using electron-reload module

I am getting the following error while trying to use the electron-reload module. (which I am not able to understand)
Error: Cannot find module 'app'
at Module._resolveFilename (module.js:455:15)
at Function.Module._resolveFilename (C:\path\Electron\music-player\node_modules\electron\dist\resources\electron.as
ar\common\reset-search-paths.js:35:12)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\path\Electron\music-player\node_modules\electron-reload\main.js:1:90)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
And the main.js has the following for usage of the module
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
require('electron-reload')(__dirname+'/public')
Can anyone guide me how to resolve the problem ?
Looks like you need to update electron-reload to v1.0.0 or later, the release notes claim it was updated to support Electron v1.0.0+.

Handling dependencies not installed with npm, in Mocha?

I have a working Node application and I'm trying to add Mocha tests, but getting some odd import errors.
This is my file structure:
package.json
index.js
src/
chart.js
test/
test_chart.js
This is what my chart.js file looks like:
global.jQuery = require('jquery');
global.$ = global.jQuery;
require('typeahead');
require('bloodhound');
var bootstrap = require('bootstrap');
var Handlebars = require('handlebars');
var Highcharts = require('highcharts-browserify');
var parse = require('csv-parse');
var moment = require('moment');
var analyseChart = {
doSomething: function() { ... }
};
module.exports = analyseChart;
Currently I import everything from /src into a single index.js file, then bundle it with browserify, which works just fine, no errors in the application.
I have a section in package.json that defines dependencies not available via npm as follows:
"browser": {
"chosen": "./vendor/chosen.jquery.min.js",
"typeahead": "./vendor/typeahead.bundle.js",
"bloodhound": "./vendor/bloodhound.js"
}
Now I want to start writing Mocha tests for the functions in /src.
This is my first stub in test_chart.js:
var chart = require('../src/chart');
chart.doSomething();
But when I run mocha, I get the following error:
/Users/.../js/node_modules/typeahead/node_modules/dom/lib/matches.js:2
var proto = Element.prototype;
^
ReferenceError: Element is not defined
at Object.<anonymous> (/Users/.../js/node_modules/typeahead/node_modules/dom/lib/matches.js:2:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/node_modules/typeahead/node_modules/dom/index.js:4:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/node_modules/typeahead/typeahead.js:3:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/src/chart.js:3:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/test/test_chart.js:3:13)
How can I fix this import error for Mocha?
I think maybe it doesn't like require('typeahead') because it can't see the browser dependency paths that I set in package.json.
Is there a way I can make these files available to Mocha as well as browserify?
Or should I use another testing package altogether?

Categories

Resources