Prettier fails when declaring { "type": "module" } in package.json - es6-modules

I have defined a prettier config in prettier.config.js.
After adding "type": "module" to package.json (to enable using import/export syntax in node), running prettier fails with the following error:
Checking formatting...
[error] Invalid configuration file `prettier.config.js`: Must use import to load ES Module: /your/project/path/prettier.config.js
[error] require() of ES modules is not supported.
[error] require() of /your/project/path/prettier.config.js from /your/project/path/node_modules/prettier/third-party.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
[error] Instead rename prettier.config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /your/project/path/package.json.
[error]
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I'm using SublimeText 3 with JSPrettier, and it stops working too since prettier doesn't work.

This happens because adding "type": "module" in package.json makes all .js files ES modules, and then they can only be imported in other ES modules.
Prettier is not able to import prettier.config.js because it is now an ES module.
To fix this, avoid having a .js file that prettier tries to import from. Instead, you can define the prettier config in a non-.js file. From the prettier docs there are several options, such as using a .prettierrc.json file or just putting the prettier config into package.json.

Related

gulp-sharp-responsive with require

when i try to use require("gulp-sharp-responsive") in gulpfile.js i get an Error:
Something went wrong installing the "sharp" module. The specified procedure cannot be found. myproject\node_modules\gulp-sharp-responsive\node_modules\sharp\build\Release\sharp.node
But if i use import sharpResponsive from "gulp-sharp-responsive" and "type": "module" in package.json it's work. What can I do to use require in gulpfile.js?
My gulpfile.js without gulp-sharp-responsive work fine.
gulp: 4.0.2, gulp-cli: 2.3.0, gulp-sharp-responsive: 0.4.0, npm: 8.11.0, node: 16.16.0
Removing "node_modules/sharp" and "npm install --ignore-scripts=false --verbose sharp", did not help.

Webpack can't import a .json file when using alias / aliased paths

I am using Webpack's native json importer to import a json file as a javascript object:
import config from "./config.json"
Works fine - problem is, when I add an alias to the webpack configuration:
resolve: {
alias: {
"#data": path.resolve(__dirname, "src/js/data/"),
},
},
Importing the json file via the aliased path does not seem to work anymore:
import config from "#data/config.json
ERROR in ...
Module not found: Error: Can't resolve '#alias/file.json' in ...
Is there any trick to get Webpack's native json importer to play nice with aliased paths?
I had to add the extensions field to the resolve configuration and include ".json" in the array - after doing that both import config from "#data/config (as the extensions is resolved automatically) and import config from "#data/config.json seem to work fine.

Importing angular-translate imports an empty object

I'm using JSPM 0.16.42 which uses SystemJS, I've tried both angular-translate and angular-route, both of which are on github endpoints.
However, for both of them angular throws the same error
argument module is not a function
when using them in ES6 syntax as follows:
import AngularRoute from 'angular-route';
angular.module('app', [AngularRoute]);
I'm using babel as a transpiler. The object I get back from the import seems to be empty. Following is the relevant part of my config.js file:
System.config({
baseURL: "/",
defaultJSExtensions: true,
transpiler: "babel",
babelOptions: {
"optional": [
"runtime",
"optimisation.modules.system"
]
},
paths: {
"github:*": "jspm_packages/github/*",
"npm:*": "jspm_packages/npm/*"
},
map: {
"angular-route": "github:angular/bower-angular-route#1.5.8",
"angular-translate": "github:angular-translate/bower-angular-translate#2.11.1",
}...
EDIT: When I tried to install angular-translate with an npm endpoint I got the error http://errors.angularjs.org/1.5.8/$injector/unpr?p0=e
on the following line in angular.js
return new ErrorConstructor(message);
which I guess is a bit of progress but still doesn't solve my issue obviously
EDIT#2: I got angular-route to work with the help of #artem by using the npm endpoint, for some reason the github endpoint did not work so I used jspm install npm:angular-route. Further investigation is needed as to why the github package did not work and the npm package did
EDIT#3: I have overriden the package configuration as you can see below, though that didn't help
"npm:angular-translate#2.11.1": {
"format": "global",
"dependencies": {
"angular": ">=1.2.26"
},
"shim": {
"angular-translate": {
"deps": "angular"
}
}
}
I eventually fixed it by installing angular-translate as well as angular-route via their npm endpoints instead of the default (github) endpoints, using
jspm install npm:angular-route
&
jspm install npm:angular-translate -o '{dependencies: { angular: ">=1.2.26" } }'
The override for angular-translate was needed since jspm did not understand the original dependency syntax correctly which was angular: ">= 1.2.26 <=1.6" as described in this github issue
Here is not-so-minimal, not-really-self-contained example for angular-translate with systemjs:
npm install jspm
./node_modules/.bin/jspm install github:angular-translate/angular-translate
keep pressing <ENTER>, accepting all the default values
create file test.js
import AngularTranslate from 'angular-translate/angular-translate';
console.log(AngularTranslate);
create file index.html
<!doctype html>
<html>
<head>
<script src="jspm_packages/system.src.js"></script>
<script src="config.js"></script>
<script>
System.import('./test.js');
</script>
</head>
<body>
</body>
</html>
open it in the browser:
Failed to load resource: the server responded with a status of 404 (File not found)
undefined:1 Uncaught (in promise) Error: (SystemJS) XHR error (404 File not found) loading
http://localhost:8035/jspm_packages/github/angular-translate/angular-translate#2.11.1.js(…)
Why that file isn't there? It's supposed to be created by jspm, if you had installed angular-translate from npm it would have contained
module.exports = require("npm:angular-translate#2.11.1/dist/angular-translate.js");
which is just a redirect from symbolic package name (the name of that .js file) to that package main file, as specified in package.json:
"main": "dist/angular-translate.js",
But angular-translate is from github, there is no dist there. That's why jspm did not create the redirect file - there is nothing to redirect to.
No problem, just build it from the source we got from github:
cd jspm_packages/github/angular-translate/angular-translate#2.11.1/
npm install
npm run-script build
cd ../../../..
and fix mapping in config.js:
map: {
"angular-translate/angular-translate": "github:angular-translate/angular-translate#2.11.1/dist/angular-translate",
open index.html in the browser again:
system.src.js:122 Uncaught (in promise) Error: (SystemJS) angular is not defined(…)
No problem, angular is already installed as angular-translate dependency, just tell systemjs when and how to load it.
add to config.js:
meta: {
"angular-translate/angular-translate": {
"deps": ["angular"]
}
},
map: {
"angular": "github:angular-translate/angular-translate#2.11.1/node_modules/angular/angular",
NOTE: You don't need to specify format for angular-translate. SystemJS auto-detects it correctly - it could be loaded as either 'amd' or 'cjs', but it will not work as 'global'. Also, you probably did not start by installing angular-translate, so you already have angular.js file and mapping in place somewhere.
Open index.html in the browser again. It prints in the console:
pascalprecht.translate
Yes angular-translate exports a string - seems to be typical for angular1 modules.
I have no experience with angular.js, so I declare it a success and stop here.
PS Why angular-route worked when you install it from npm, and did not work from github?
When installed from npm, jspm created a file named jspm_packages/npm/angular-route#1.5.8.js, containing
module.exports = require("npm:angular-route#1.5.8/index.js");
because package.json for angular-route has
"main": "index.js",
which is correct and works for you.
When installed from github, jspm created similar file jspm_packages/github/angular/bower-angular-route#1.5.8.js, but this time it points to a different file
module.exports = require("github:angular/bower-angular-route#1.5.8/angular-route");
because someone put an override in jspm registry there at https://github.com/jspm/registry/blob/master/package-overrides/github/angular/bower-angular-route%401.3.0.json
because bower.json for bower-angular-route has
"main": "./angular-route.js",
Maybe it's an oversight, maybe it's correct and works for them - I don't know.
TL;DR It's not a good idea to use package manager for installing software, if the software was not packaged properly for that package manager.

bower.json and package.json interpolation

I got a project from 3rd party and they have something like below in bower.json and package.json file
"name": "${project.artifactId}",
"description": "${project.description}",
"version": "${project.version}",
I cannot figure out which build engine uses ${} interpolation in .json file. There is also brunch-config.cofee file with similar usage. I don't see anything describing those variables. My brunch build just fail completely (maybe for unrelated reason):
$ brunch build -d
14 Aug 23:03:50 - error: { [Error: Component must have "/Users/test/bower/angular/bower.json"] code: 'NO_BOWER_JSON' }
brunch:watch Loaded plugins: +0ms
brunch:watch File 'package.json' received event 'add' +11ms
brunch:watch File 'bower.json' received event 'add' +2ms
brunch:watch File 'brunch-config.coffee' received event 'add' +0ms
Can you help to provide some guidance?
Thanks
Probably they used Maven. It looks like the Maven Model Interpolation. Look at the configuration docs to see where these configurations reside

Load new module in Kamailio

I would like to ask, how can I load new module in Kamailio 4.1.2?
Actually, I have an issue, when I tried to compile my kamaiio.cfg
I've got error:
root#kamailio:/usr/local/# kamailio -c kamailio.cfg
loading modules under /usr/local/lib64/kamailio/modules/
0(25392) ERROR: <core> [sr_module.c:587]: load_module(): ERROR: load_module: could not find module <websocket> in </usr/local/lib64/kamailio/modules/>
0(25392) : <core> [cfg.y:3408]: yyerror_at(): parse error in config file /usr/local/etc/kamailio/kamailio.cfg, line 323, column 12-25: failed to load module
0(25392) ERROR: <core> [cfg.y:3272]: yyparse(): cfg. parser: failed to find command ws_handle_handshake
0(25392) : <core> [cfg.y:3411]: yyerror_at(): parse error in config file /usr/local/etc/kamailio/kamailio.cfg, line 1083, column 27: unknown command, missing loadmodule?
ERROR: bad config file (2 errors)
when I look physically to the /usr/local/lib64/kamailio/modules/ there is some modules, but websocket.so is missing.
So, how can I get and load module in Kamailio?
Thank you for help!
You haven't installed the module websocket. Edit modules.lst file in the source code directory and add websocket to include_modules variable. If you don't have modules.lst, just do:
make cfg
Alternative is to do:
make cfg include_modules="websocket"
By default, the build system for kamailio compiles and install only the modules that have the same dependencies as the core of the application. For websocket you need to install libunistring and openssl (libssl) devel packages.
Steps to load new module to Kamailio server. (Try if above answer is not working for you)
Check the modules is exist in the default module directly /usr/local/lib64/kamailio/modules.
If found, add loadmodule "module_name.so" in load module section in kamailio.cfg file.
If the module is not found in default module directory, you can check for the source code of that module in the default module source code directory /usr/local/src/kamailio-4.4/kamailio/modules.
If source code found, enter to the module directory. Then create modules' shared object file(.so) by following commands.
./configure
make
make test
make install
Then you will get a shared object file(.so). Copy that file into the default module directory. and load this module from the kamailio.cfg file as mentioned in step 1.
If module source code does not exist in the default source code directory, You need to download the source code from the web. And follow step 3 and 4.