Load JSON file using node require and absolute path fails - javascript

I'm trying to require a JSON file using require. But it fails miserably though the file does exist. I'm wondering if it's because I try to load a JSON file instead of a JS file there, as I usually do.
error: verbose: Evaluating c:\wamp\www\Ayolan\Translation\config/locales/languages.json as module "c:\wamp\www\Ayolan\Translation\config/locales/languages.json" failed with error: SyntaxError: Unexpected token :
It seems that it doesn't like the : character in the windows path. What solution do I have here? I cannot use a relative path to the file because it's a shared file that is loading a file that depends on the application config.
var pathLanguages = __config.path.base + __config.path.languages;
var languages = require(pathLanguages + 'languages.json');
I believe loading JS file using this method would work. (I've just made the test and it does work with JS file)
Using node 0.10.24
JSON file:
{
"fr": {
"min": "fr",
"lang": "fr-fr",
"language": "Français"
},
"en": {
"min": "en",
"lang": "en-en",
"language": "English"
}
}
Error message:
error: Error: Evaluating c:\wamp\www\Ayolan\Translation\config/locales/fr.json as module "c:\wamp\www\Ayolan\Translation\config/locales/fr.json" failed with error: SyntaxError: Unexpected token :
at Function.req.load (c:\wamp\www\Ayolan\Translation\node_modules\requirejs\bin\r.js:2505:23)
at Function.req.get (c:\wamp\www\Ayolan\Translation\node_modules\requirejs\bin\r.js:2455:29)
at localRequire (c:\wamp\www\Ayolan\Translation\node_modules\requirejs\bin\r.js:1632:40)
at c:\wamp\www\Ayolan\Translation\shared\app\Lang.js:36:48
at forOwn (c:\wamp\www\Ayolan\Translation\node_modules\lodash\dist\lodash.js:2105:15)
at Function.forEach (c:\wamp\www\Ayolan\Translation\node_modules\lodash\dist\lodash.js:3302:9)
at Function.Lang._loadAllLanguages (c:\wamp\www\Ayolan\Translation\shared\app\Lang.js:34:15)
at Object.module.exports.bootstrap (c:\wamp\www\Ayolan\Translation\config\bootstrap.js:28:12)
at Sails.runBootstrap (c:\wamp\www\Ayolan\Translation\node_modules\sails\lib\app\private\bootstrap.js:46:27)
at Sails.bound [as runBootstrap] (c:\wamp\www\Ayolan\Translation\node_modules\lodash\dist\lodash.js:729:21) { [Error: Evaluating c:\wamp\www\Ayolan\Translation\config/locales/fr.json as module "c:\wamp\www\Ayolan\Translation\config/locales/fr.json" failed with error: SyntaxError: Unexpected token :]
originalError: [SyntaxError: Unexpected token :],
moduleName: 'c:\\wamp\\www\\Ayolan\\Translation\\config/locales/fr.json',
requireModules: [ 'c:\\wamp\\www\\Ayolan\\Translation\\config/locales/fr.json' ],
fileName: 'c:\\wamp\\www\\Ayolan\\Translation\\config/locales/fr.json' }

Related

"Uncaught Error: Cannot find module 'url"' by using matrix-js-sdk

The error is:
Uncaught Error: Cannot find module 'url'
I my only code is:
import sdk from 'matrix-js-sdk';
const client = sdk.createClient("https://matrix.org");
client.login("m.login.password", {"user": "Jak2k", "password": localStorage.getItem("pass")}).then((response) => {
console.log(response.access_token);
});
Before that, I just installed it with npm.(Maybe it's important, that I use react in the same package.)

Extracting terraform resource changes with github-script#v5 action

I have a github workflow where I want to filter the terraform destroy changes from the terraform plan file and post that as comment in the PR.
- name: Terraform Plan
id: plan_json
run: |
terraform plan -out planfile 2>error.log
terraform show -json planfile > plan.json
continue-on-error: true
- uses: actions/github-script#v5
id: message
if: ${{ always() }}
with:
result-encoding: string
script: |
const fs = require('fs');
const report = JSON.parse(fs.readFileSync('./plan.json'));
var message = '';
for (const changes in report.resource_changes) {
message += `${changes.change.actions[0]} ${changes.name} (${changes.type})\n`
};
console.log('Message: ', message);
return message;
When I run the workflow it gives this error:
SyntaxError: Unexpected token c in JSON at position 1
at JSON.parse (<anonymous>)
at eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4942:56), <anonymous>:4:21)
at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4943:12)
Error: Unhandled error: SyntaxError: Unexpected token c in JSON at position 1
at main (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4997:26)
at Module.272 (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:4981:1)
at __webpack_require__ (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:24:31)
at startup (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:43:19)
at /home/runner/work/_actions/actions/github-script/v5/dist/index.js:49:18
at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v5/dist/index.js:52:10)
at Module._compile (internal/modules/cjs/loader.js:959:30)
I don't have any nodejs/javascript experience so I have no clue what I am doing wrong here.
The actual plan file can be found here.
When I run the nodejs script locally it works.
❯ node tfplan.js
Message: create, rg (azurerm_resource_group)
create, rg-name (random_pet)
- uses: hashicorp/setup-terraform#v1
with:
terraform_wrapper: false
terraform_wrapper: false had to be added to the terraform setup task.
For more info check this.

Angular 8 - Getting Runtime compiler not loaded error

I need to dynamically create lazy loaded routes. When I use import syntax, e.g. loadChildren: () => import(`./pages/${module.moduleName}/${module.moduleName}.module`).then(m => m[childModuleName]), it works on JIT but when running on AOT, it throws this error:
ERROR Error: Uncaught (in promise): Error: Runtime compiler is not loaded as per https://github.com/angular/angular-cli/issues/10582 the solution would be to use the old string syntax:
```` loadChildren: ./pages/${module.moduleName/${module.moduleName}.module#${childModuleName},
I get this error:
``` ERROR Error: Uncaught (in promise): Error: Cannot find module './pages/client-migration/client-migration.module.ngfactory' ```
import the module name into your module.ts.

Syntax error on async function as object field

I'm have a problem on the simple Getting started tutorial of FeathersJS.
I copy/past both code and run the server, but it return me a Syntax Error on client.js line 5:
app.use('todos', {
async get(name) {
^^^^^^^^^^^^^^^^^
> Uncaught SyntaxError: Unexpected identifier
What did I miss ?
EDIT: I forgot to say that error come from the browser after I run
http-server public/

Using libxml-xsd in an electron app fails with bindings error

I would like to validate an XML file based upon its schema in an electron application.
I have an angular2 service which works before the inclusion of my libxml-xsd require statement. As soon as I add this to the top of my service file:
const xsd = require('libxml-xsd');
I run npm run build which executes this :
webpack --progress --profile --colors --display-error-details --display-cached
I see the following new warnings in the output:
WARNING in ./~/bindings/bindings.js
Critical dependencies:
76:22-40 the request of a dependency is an expression
76:43-53 the request of a dependency is an expression
# ./~/bindings/bindings.js 76:22-40 76:43-53
WARNING in ./~/bindings/README.md
Module parse failed: /Users/myusername/Documents/Personal/MyApp/node_modules/bindings/README.md Unexpected token (2:3)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:3)
at Parser.pp$4.raise (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp.unexpected (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:603:10)
at Parser.pp$3.parseExprAtom (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1822:12)
at Parser.pp$3.parseExprSubscripts (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1715:21)
at Parser.pp$3.parseMaybeUnary (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1692:19)
at Parser.pp$3.parseExprOp (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1656:43)
at Parser.pp$3.parseExprOp (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1658:21)
at Parser.pp$3.parseExprOps (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1639:17)
at Parser.pp$3.parseMaybeConditional (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1597:21)
at Parser.pp$3.parseExpression (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:1573:21)
at Parser.pp$1.parseStatement (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:727:47)
at Parser.pp$1.parseTopLevel (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:638:25)
at Parser.parse (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:516:17)
at Object.parse (/Users/myusername/Documents/Personal/MyApp/node_modules/acorn/dist/acorn.js:3098:39)
at Parser.parse (/Users/myusername/Documents/Personal/MyApp/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/Users/myusername/Documents/Personal/MyApp/node_modules/webpack/lib/NormalModule.js:104:16)
at DependenciesBlock.onModuleBuild (/Users/myusername/Documents/Personal/MyApp/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/Users/myusername/Documents/Personal/MyApp/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
at /Users/myusername/Documents/Personal/MyApp/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
at Storage.finished (/Users/myusername/Documents/Personal/MyApp/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
at /Users/myusername/Documents/Personal/MyApp/node_modules/graceful-fs/graceful-fs.js:78:16
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:446:3)
# ./~/bindings ^\.\/.*$
I then run my application by executing : electron main.js which then throws this error in the browsers console:
Uncaught Error: Could not locate the bindings file. Tried:
→ /Users/myusername/Documents/Personal/MyApp/build/xmljs.node
→ /Users/myusername/Documents/Personal/MyApp/build/Debug/xmljs.node
→ /Users/myusername/Documents/Personal/MyApp/build/Release/xmljs.node
→ /Users/myusername/Documents/Personal/MyApp/out/Debug/xmljs.node
→ /Users/myusername/Documents/Personal/MyApp/Debug/xmljs.node
→ /Users/myusername/Documents/Personal/MyApp/out/Release/xmljs.node
→ /Users/myusername/Documents/Personal/MyApp/Release/xmljs.node
→ /Users/myusername/Documents/Personal/MyApp/build/default/xmljs.node
→ /Users/myusername/Documents/Personal/MyApp/compiled/6.5.0/darwin/x64/xmljs.node
Could somebody please help me get libxml-xsd running under my application configuration? Does this have anything to do with webpack? If so - how do I change its config to bring in the appropriate files?
You can resolve issue with bindings.js using this code from GitHub issue.
webpack
module.exports = {
plugins: [
new webpack.IgnorePlugin(/^mock-firmata$/),
new webpack.ContextReplacementPlugin(/bindings$/, /^$/)
],
externals: ["bindings"]
}
The problem is that libxml-xsd package is for server side. I think you will get another problem after this one. It has dependencies like this require('fs'); which can't be used in front-end.
I think you need to use either the asar-unpack or asar-unpack-dir parameters on the folders containing native modules.
Looks like it only takes one string, see the API docs for asar-unpack-dir for glob examples.
(The one string limitation appears to be an asar module limitation, if you think it should work as an array, you'd need to file an issue in their repository.)

Categories