Problem with Node.JS and jsconfig.json with absolute imports - javascript

IDE: Replit
Good afternoon, thanks for checking out.
I wanted to implement absolute imports to my node project, but something odd is happening whenever I try to run the code.
Here are the things I set for my jsconfig.json file:
{
"compilerOptions": {
"baseUrl": "src"
}
}
Now, this is how i'm calling the file I need inside another one:
const {title} = require('util.js')
Oddly, replit's intellisense shows up the right path:
Thumbs Up From Replit
But then, when I finally execute the code, I get this error message:
Error: Cannot find module '/home/runner/Moniqueta-Discordjs/node_modules/util.js/index.js'. Please verify that the package.json has a valid "main" entry
at tryPackage (node:internal/modules/cjs/loader:353:19)
at Function.Module._findPath (node:internal/modules/cjs/loader:566:18)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (/home/runner/Moniqueta-Discordjs/src/commands/rps.js:2:17)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) {
code: 'MODULE_NOT_FOUND',
path: '/home/runner/Moniqueta-Discordjs/node_modules/util.js/package.json',
requestPath: 'util.js'
}
I tried following the error by adding a "valid main entry to my package.json" file, like this:
{
"name": "moniqueta-discordjs",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"keywords": [],
"author": "sum117",
"license": "ISC",
"dependencies": {
"#replit/database": "^2.0.2",
"discord.js": "^13.8.0",
"src": "^1.1.2",
"util.js": "^0.0.0"
}
}
And when I checked it, it had "util.js" as a declared dependency, I really don't understand what's going on.
Thanks for reading. What am I missing? I really would love this to work...
If my explanation is not enough, you can access my open source code via the link.

Related

anchor test command error - Error: Cannot find module '[#project-serum/anchor] ' Please verify that the package.json has a valid "main" entry

I was following an introduction tutorial using anchor (solana framework), it is based in the official github.
In the testing part, I used anchor test command and I got this error:
BPF SDK: /home/sebastian/.local/share/solana/install/releases/1.9.12/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
Finished release [optimized] target(s) in 3.86s
cargo-build-bpf child: /home/sebastian/.local/share/solana/install/releases/1.9.12/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /home/sebastian/Documentos/solana-project/anchor/examples/tutorial/basic-1/target/deploy/basic_1.so
To deploy this program:
$ solana program deploy /home/sebastian/Documentos/solana-project/anchor/examples/tutorial/basic-1/target/deploy/basic_1.so
The program address will default to this keypair (override with --program-id):
/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/basic-1/target/deploy/basic_1-keypair.json
yarn run v1.22.17
$ /home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/.bin/mocha -t 1000000 tests/
Error: Cannot find module '/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/#project-serum/anchor/dist/cjs/index.js'. Please verify that the package.json has a valid "main" entry
at tryPackage (node:internal/modules/cjs/loader:353:19)
at Function.Module._findPath (node:internal/modules/cjs/loader:566:18)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/basic-1/tests/basic-1.js:2:16)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
at async formattedImport (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
at async Object.exports.requireOrImport (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/nodejs/esm-utils.js:48:32)
at async Object.exports.loadFilesAsync (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/nodejs/esm-utils.js:88:20)
at async singleRun (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/cli/run-helpers.js:125:3)
at async Object.exports.handler (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/cli/run.js:374:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
This is my package.json:
{
"name": "basic-1",
"version": "0.24.2",
"license": "(MIT OR Apache-2.0)",
"homepage": "https://github.com/project-serum/anchor#readme",
"bugs": {
"url": "https://github.com/project-serum/anchor/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/project-serum/anchor.git"
},
"engines": {
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
},
"dependencies": {
"#project-serum/anchor": "^0.24.2"
}
}
I really appreciate any information about it. I also attach the tutorial and anchor's github link.
https://github.com/project-serum/anchor
https://youtu.be/CmG5_sIas1Q
Try re-installing #project-serum/anchor
yarn add #project-serum/anchor
In your anchor.toml file, try changing the value of 'test' to mocha tests/test.js , replace test.js with whatever the name of your file in tests.
Go check node_modules/#project-serum/anchor if you don't have a "dist" folder there you will have to run yarn add#project-serum/anchor again. When you can see that the "dist" folder was properly installed you'll be able to run your tests again.

Using a dependency with an 'export' statement breaks at Electron app startup

I'm trying to use electron-webpack to build an electron app with atlaskit.
I've setup the smallest possible repo to reproduce the issue: fstephany/bug-report-electron-webpack.
Here's my package.json:
{
"name": "electron-webpack-quick-start",
"version": "0.0.0",
"license": "MIT",
"esm": "auto",
"scripts": {
"dev": "electron-webpack dev",
"compile": "electron-webpack",
"dist": "yarn compile && electron-builder",
},
"dependencies": {
"source-map-support": "^0.5.12",
"esm": "^3.2.25",
"#atlaskit/navigation-next": "6.3.0"
},
"devDependencies": {
"electron": "5.0.4",
"electron-builder": "^20.44.4",
"electron-webpack": "^2.7.1",
"webpack": "~4.35.0"
}
}
And my src/renderer/index.js which simply loads the external dependency:
import { LayoutManager } from "#atlaskit/navigation-next";
let app = document.getElementById("app");
let layoutManager = LayoutManager;
console.log(app);
console.log(layoutManager);
When I run the app with $ yarn dev. I get an error in the web console from the Electron window:
Uncaught /home/fstephany/Code/Play/new-electron-webpack-project/node_modules/#atlaskit/navigation-next/index.js:2
export { default as ContainerHeader } from './components/presentational/ContainerHeader';
^^^^^^
SyntaxError: Unexpected token export
at new Script (vm.js:85:7)
at createScript (vm.js:266:10)
at Object.runInThisContext (vm.js:314:10)
at Module._compile (internal/modules/cjs/loader.js:742:26)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:820:10)
at Module.load (internal/modules/cjs/loader.js:677:32)
at tryModuleLoad (internal/modules/cjs/loader.js:609:12)
at Function.Module._load (internal/modules/cjs/loader.js:601:3)
at Module.require (internal/modules/cjs/loader.js:715:19)
at require (internal/modules/cjs/helpers.js:14:16)
SyntaxError: Unexpected token export makes me think that something is missing from the Babel transpiling. Especially when the release note of #atlaskit/navigation-next version 5.0.0 explicitly mentions:
Drop ES5 from all the flow modules
Dropping CJS support in all #atlaskit packages
As a breaking change, all #atlaskit packages will be dropping cjs
distributions and will only distribute esm. This means all distributed
code will be transpiled, but will still contain import and export
declarations.
Any idea on how to fix this? I've tried different babel config but I feel like turning in circle...
I made it work by puttin the atlaskit dependency in the whilelistedmodule settings of electron-webpack. See the commit for context.
"electronWebpack": {
"whiteListedModules": ["#atlaskit/navigation-next"]
},

Angular app fails to run and displays invalid config error

I am building my angular app from the angular cli starter.
The problem is being suggested in the error below.
C:\Users\Afropepe\Desktop\development\buy-ticket\node_modules\#angular\cli\models\config\config.js:76
throw new InvalidConfigError(common_tags_1.stripIndent
^ InvalidConfigError: Parsing 'C:\Users\Afropepe\Desktop\development\buy-ticket.angular-cli.json'
failed. Ensure the file is valid JSON. Error: Unexpected token ] in
JSON at position 535
at InvalidConfigError (C:\Users\Afropepe\Desktop\development\buy-ticket\node_modules\#angular\cli\models\config\config.js:11:9)
at Function.fromConfigPath (C:\Users\Afropepe\Desktop\development\buy-ticket\node_modules\#angular\cli\models\config\config.js:76:19)
at Function.fromProject (C:\Users\Afropepe\Desktop\development\buy-ticket\node_modules\#angular\cli\models\config.js:88:46)
at Object. (C:\Users\Afropepe\Desktop\development\buy-ticket\node_modules\#angular\cli\commands\build.js:7:35)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
Please ensure your file named ".angular-cli.json" contains JSON format, in my case I added some comments on the JSON file, and even when VS compile, this is not valid
"assets": [ "assets"],
//"index": "myindexfile.html",
"main": "main.ts",
Please make sure your file is has a right JSON format
The error that I was getting is below:
\#angular\cli\models\config\config.js:89
throw new InvalidConfigError(common_tags_1.stripIndent `
^
I had some error, when tried to import in angular-cli.json in scripts file
"../node_modules/tether/dist/js/tether.min.js"
after
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
So maybe this problem occurs when the one library needs to include another library before it.
angular-cli.json Content:
//"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
Inside the .angular-cli.json/ angular-cli.json just remove ////"index": "index.html", because it won't considering as comment and throw some exception.
Correct Output:
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
I had same error, resolved by replacing with new angular-cli.json and reconfigure.

Electron Cannot find module /resources/app/index.js

I'm new to electron and have made my first electron application, it runs normally running npm start but when using npm run I am unable to execute my application. (I am running ubuntu linux) The cli gives this error message when running the executable file:
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module '/home/user/Documents/dev/cocoa/cocoa-linux-x64/resources/app/index.js'
at Module._resolveFilename (module.js:455:15)
at Function.Module._resolveFilename (/home/user/Documents/dev/cocoa/cocoa-linux-x64/resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._load (module.js:403:25)
at Object.<anonymous> (/home/user/Documents/dev/cocoa/cocoa-linux-x64/resources/electron.asar/browser/init.js:171:8)
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)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
This is my package.json :
{
"name": "cocoa",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "electron main.js",
"package-linux" : "electron-packager . --platform=linux --arch=x64 --electron-version=1.4.13"
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron-packager": "^8.7.1"
}
}
this is the main.js file :
const electron = require('electron')
const {app, BrowserWindow} = electron
app.on('ready', ()=> {
let win = new BrowserWindow({
width:400,
height:400
})
win.loadURL('http://google.com')
})
Does anyone know what I am doing wrong, thank you for your help.
Ok, that was really stupid. I had named the main js file for the project main.js when in actual fact it had to be named index.js
problem solved!

Node.js "Error: Can not find module"

I am doing the node.js lesson from lynda.com and am unable to find what is causing the "Error: Can not find module". From everything I can tell the package.json has the flight module in it.
Everything has been working so far so I know node.js is installed and running correctly. The way I am running this file is by typing "node app.js" at the command line. Also all of the files are in the same folder.
Here is the console log:
C:\Users\Jonathan\Desktop\flight>node app.js
module.js:340
throw err;
^
Error: Cannot find module './flight'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (C:\Users\Jonathan\Desktop\flight\app.js:1:76)
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 Function.Module.runMain (module.js:497:10)
C:\Users\Jonathan\Desktop\flight>
Here is the code.
Package.json file
{
"name": "flight",
"version": "1.0.0",
"description": "a module for keeping track of a flight",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "JBaxter",
"license": "ISC"
}
index.js file
var number, origin, destination;
exports.setNumber = function (num){
number = num;
};
exports.setOrigin = function (o){
origin = o;
};
exports.setDestination = function (d){
destination = d;
};
exports.getInfo = function() {
return {
number: number,
origin: origin,
destination: destination
};
};
app.js file
var flight = require('./flight');
flight.setOrgin('LAX');
flight.setDestination('DCA');
flight.setNumber(462);
console.log(flight.getInfo());
Any help with be great. I thought the name within the package.json file was your module but maybe I am incorrect. Thanks in advance.
"./flight" does not evaluate to index.js in your case.
If you create a subdir :
flight
- index.js
then you can require("./flight") and have it evaluated to "./flight/index.js".
In your case, simplest thing you could to do :
require("./index.js");
you can do it in the following way:
var flight = require('./index.js');
OR
var flight = require('./index');
Following code will not work because the node will look for module named 'flight' in node_modules folder
var flight = require('flight');

Categories

Resources