Resolve "Cannot find package" error using Node.js - javascript

I'm trying to import a module using Node.js. When I specify the path I'm importing from, it works perfectly fine. Code below runs without returning any error
import myFunc from("./lib/myPkg.js");
import pkg from ("root/node_modules/date-names/index.js");
const {days, months} = pkg;
However, I get this error when I try to import an npm package by calling import ordinal from "ordinal"
root#username:/path/to/main.mjs# node main.mjs
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'ordinal' imported from /path/to/main.mjs
at packageResolve (internal/modules/esm/resolve.js:675:9)
at moduleResolve (internal/modules/esm/resolve.js:716:14)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:809:11)
at Loader.resolve (internal/modules/esm/loader.js:85:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:51:40)
at link (internal/modules/esm/module_job.js:50:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
I have tried installing the package globally but I still can't get it to work.

Related

SyntaxError: Cannot use import statement outside a module in kochava using Next.js

error - SyntaxError: Cannot use import statement outside a module
C:\xampp\htdocs\next-contv-web\node_modules\kochava\dist\kochava.js:17
import { Log } from "./utils/log";
react JS version being used: "react": "17.0.2"
next JS version: "12",
kochava version being used: "kochava": "^3.0.4"
Getting the above error msg when installed the kochava latest version in next/react (web sdk). Need help to resolve this issue.
Getting the below error after adding "type":"module" in the package.json of kochava and running the app again:
error - Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\xampp\htdocs\next-contv-web\node_modules\kochava\dist\utils\log' imported from C:\xampp\htdocs\next-contv-web\node_modules\kochava\dist\kochava.js
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\xampp\htdocs\next-contv-web\node_modules\kochava\dist\utils\log' imported from C:\xampp\htdocs\next-contv-web\node_modules\kochava\dist\kochava.js
at new NodeError (node:internal/errors:372:5)
at finalizeResolution (node:internal/modules/esm/resolve:437:11)
at moduleResolve (node:internal/modules/esm/resolve:1009:10)
at defaultResolve (node:internal/modules/esm/resolve:1218:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)
at link (node:internal/modules/esm/module_job:78:36) {
code: 'ERR_MODULE_NOT_FOUND',
page: '/'
}

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express' imported from F:\Document\My Project\NODE JS\Sample 1\tinder-backend\server.js

This is the error. How I fix this....
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express' imported from F:\Document\My Project\NODE JS\Sample 1\tinder-backend\server.js
at new NodeError (node:internal/errors:371:5)
at packageResolve (node:internal/modules/esm/resolve:884:9)
at moduleResolve (node:internal/modules/esm/resolve:929:18)
at defaultResolve (node:internal/modules/esm/resolve:1044:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
at ModuleWrap. (node:internal/modules/esm/module_job:76:40)
at link (node:internal/modules/esm/module_job:75:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
You are missing a module 'express'.
You have to install it with:
npm i -s express
this will install express and save it in package.json

Error: A dynamic link library (DLL) initialization routine failed on electron but it's fine on node js

I'm trying to load a custom module in electron written in D with node_dlang package, which is fine with node, but it fails within electron.
the test with node, that runs just fine, goes like this:
const nativeModule = require('./module.node');
const assert = require('assert');
assert(nativeModule != null);
assert(nativeModule.ultimate() == 42);
But when I went to use it within electron.js, through the preload script, it returns in an error.
the preload script goes like this:
const {
contextBridge,
ipcRenderer
} = require("electron");
const nativeModule = require('./module.node');
const assert = require('assert');
assert(nativeModule.ultimate() == 42);
function pageLoaded()
{
// ...
}
window.addEventListener('DOMContentLoaded', pageLoaded);
the error when I attempt to load the module within electron application is:
A JavaScript error occured in the browser process
--------------------------- Uncaught Exception: Error: A dynamic link library (DLL) initialization routine failed.
\\?\C:\Users\001\Desktop\ele\module.node
at process.func [as dlopen] (VM70 asar_bundle.js:5)
at Object.Module._extensions..node (VM43 loader.js:1138)
at Object.func [as .node] (VM70 asar_bundle.js:5)
at Module.load (VM43 loader.js:935)
at Module._load (VM43 loader.js:776)
at Function.f._load (VM70 asar_bundle.js:5)
at Function.o._load (VM75 renderer_init.js:33)
at Module.require (VM43 loader.js:959)
at require (VM50 helpers.js:88)
at Object.<anonymous> (VM88 C:\Users\001\Desktop\ele\preload.js:6)
What's causing this and how do I fix this?
version
node version is: v14.17.0
electron.js: v13.1.1
both are 64-bit.
the module source code goes like this:
import std.stdio : stderr;
import node_dlang;
extern(C):
void atStart(napi_env env)
{
import std.stdio;
writeln ("Hello from D!");
}
int ultimate()
{
return 42;
}
mixin exportToJs! (ultimate, MainFunction!atStart);
it's compiled with dub command line. No arguments.
UPDATE 1 Do I need to rebuild this module? I found this but it didn't work for me either. I installed the electron-rebuild package by npm install --save-dev electron-rebuild and rebuild with .\node_modules\.bin\electron-rebuild.cmd -v 13.1.1 the command ran fine but I still got same error.
UPDATE 2: inside the console, I clicked in the javascript source code file link in the error message (from the exception) it points to this line of code, where there's this comment saying that:
no static exports found
what does that mean? is this related to the methods in D class? they're marked as public... not sure if related
Electron is a Windows-Application and therefore you need to remove output to std. Try to remove
import std.stdio : stderr;
and
import std.stdio;
writeln ("Hello from D!");
and retry import to Electron.
Please refer this (https://stackoverflow.com/a/74280836/9558119) post from me regarding same. Since it is electron build might be missing Visual C++ Build Environment: Visual Studio Build Tools

How to add my own node.js script to Angular 4 project

I have a file function.js which exports function to comunicate with some api and return value. It comunicates using request library. I am using this in node.js app. How can I import/use it in my angular 4 app?
I thought to install this file as node_module. So I created new folder function
where is function.js, package-json and node_modules folder. and then I installed it in my ng4 app by command
npm install --save ./function
then in typings.d.ts I declare it
declare var Myfunction: any
and import it in my component
import * as Myfunction from "function"
but when i try using it I get an error :
ERROR in /Users/i343346/order-site/src/app/order/order.component.ts (37,12): Unexpected token. A constructor, method, accessor, or property was expected.
ERROR in /Users/i343346/order-site/src/app/order/order.component.ts (37,13): Function implementation is missing or not immediately following the declaration.
UPDATE
I tried #Z. Bagley answear and now i can import my file. but got error:
WARNING in ./node_modules/ajv/lib/compile/index.js 13:21-34 Critical dependency: the request of a dependency is an expression
at CommonJsRequireContextDependency.getWarnings (/Users/i343346/order-site/node_modules/webpack/lib/dependencies/CommonJsRequireContextDependency.js:27:4)
at Compilation.reportDependencyErrorsAndWarnings (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:677:24)
at Compilation.finish (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:535:9)
at applyPluginsParallel.err (/Users/i343346/order-site/node_modules/webpack/lib/Compiler.js:512:17)
at /Users/i343346/order-site/node_modules/tapable/lib/Tapable.js:289:11
at _addModuleChain (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:481:11)
at processModuleDependencies.err (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:452:13)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
WARNING in ./node_modules/ajv/lib/async.js 96:20-33 Critical dependency: the request of a dependency is an expression
at CommonJsRequireContextDependency.getWarnings (/Users/i343346/order-site/node_modules/webpack/lib/dependencies/CommonJsRequireContextDependency.js:27:4)
at Compilation.reportDependencyErrorsAndWarnings (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:677:24)
at Compilation.finish (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:535:9)
at applyPluginsParallel.err (/Users/i343346/order-site/node_modules/webpack/lib/Compiler.js:512:17)
at /Users/i343346/order-site/node_modules/tapable/lib/Tapable.js:289:11
at _addModuleChain (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:481:11)
at processModuleDependencies.err (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:452:13)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
WARNING in ./node_modules/ajv/lib/async.js 119:15-28 Critical dependency: the request of a dependency is an expression
at CommonJsRequireContextDependency.getWarnings (/Users/i343346/order-site/node_modules/webpack/lib/dependencies/CommonJsRequireContextDependency.js:27:4)
at Compilation.reportDependencyErrorsAndWarnings (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:677:24)
at Compilation.finish (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:535:9)
at applyPluginsParallel.err (/Users/i343346/order-site/node_modules/webpack/lib/Compiler.js:512:17)
at /Users/i343346/order-site/node_modules/tapable/lib/Tapable.js:289:11
at _addModuleChain (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:481:11)
at processModuleDependencies.err (/Users/i343346/order-site/node_modules/webpack/lib/Compilation.js:452:13)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
I think is connected with request library, which maybe isn't supported by browser.
A simple way to include custom javascript functions in your Angular 4 project is to include the item in your assets folder, and then use require to import it into your component typescript.
src/assets/example.js
export function test() {
console.log('test');
}
src/app/app.component.ts
(before #Component(...))
declare var require: any;
const example = require('../assets/example');
(inside export class AppComponent implements OnInit { ...)
ngOnInit() {
example.test();
}
I think you might also need to include your code in index.html via script tag.
You might find this other answer useful: https://stackoverflow.com/posts/40635697/edit

Importing the Nock npm module creates error when running npm test

If I remove the nock import I won't get an error message. I've already npm installed nock.
Test file
import configureMockStore from 'redux-mock-store'
import { promiseHandler } from '../middleware'
import * as actions from './index'
import nock from 'nock'
import expect from 'expect'
const middlewares = [promiseHandler]
const mockStore = configureMockStore(middlewares)
Error Message
FAIL src/actions/index.test.js
Runtime Error
- TypeError: debug is not a function
at overrideClientRequest (node_modules/nock/lib/intercept.js:221:3)
at activate (node_modules/nock/lib/intercept.js:320:3)
at Object.<anonymous> (node_modules/nock/lib/intercept.js:383:1)
at Runtime._execModule (node_modules/jest-cli/src/Runtime/Runtime.js:261:17)
at Object.<anonymous> (node_modules/nock/lib/recorder.js:6:17)
at Object.<anonymous> (node_modules/nock/index.js:1:135)
at Object.<anonymous> (src/actions/index.test.js:4:39)
at jasmine2 (node_modules/jest-jasmine2/src/index.js:252:16)
at handle (node_modules/worker-farm/lib/child/index.js:41:8)
at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
at emitTwo (events.js:100:13)
at process.emit (events.js:185:7)
at handleMessage (internal/child_process.js:718:10)
at Pipe.channel.onread (internal/child_process.js:444:11)
I wasn't aware that my project was using the Jest testing framework,
subsequently the module needed to be "unmocked" by doing jest.unmock('nock'); before the import.

Categories

Resources