I am trying to include local npm package in a cloud function. I used the following commands:
$ npm pack
$ npm install --save tarball-output.tgz
How do I include a packed npm package in app.js? I used:
let mypackage = require('my_package');
I getting the following error when trying to deploy:
ERROR: (gcloud.beta.functions.deploy) OperationError: code=3,
message=Function load error: Code in file app.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'my_package'
Your package.json must point to your tarball, like this:
{
"dependencies": {
"my_package": "file:tarball-output.tgz"
}
}
Related
Vue.js + node.js is the basis of my app. I wanted to use the vue-confirm-dialog package from npm but this code line leads to an error:
import VueConfirmDialog from 'vue-confirm-dialog'
I have already executed: npm install --save vue-confirm-dialog and the module vue-confirm-dialog appears in the package.json file.
The error message Module not found: Error: Can't resolve 'vue-confirm-dialog' still appears.
Had the same issue today, solved it by downgrading vue-confirm-dialog to 1.0.2.
npm install vue-confirm-dialog#1.0.2 --save
I have created a custom npm package library, I have successfully created it and published on npm and then installed the package using npm install <my-package-name>.
And now I am facing issue on importing the package.When I import it in app.module.ts file its shows:
Cannot find module 'my-module-name' or its corresponding type declarations.
I have also tried by deleting the node-modules and package-lock.json files and then npm install but sadly it's not works.
The monorepo I am using is a complete Nextjs app in itself.
For the use case I am looking for,
some_previous_project
is a package defined in package.json installed from git. I only need to import the utility functions and components from it. The project was done in es6 so transpiling is necessary for the execution. I need every component or function imported from this package to be transpiled.
The configuration I am currently using is:
const withTM = require('next-transpile-modules')(['some_previous_project']);
const withPlugins = require('next-compose-plugins');
const nextConfig = {
target: 'serverless',
webpack(config) {
return config;
},
};
module.exports = withPlugins([withTM], nextConfig);
This throws the following error:
C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project>npm run dev
> trendline-frontend#0.1.0 dev C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project
> next dev
Error: next-transpile-modules - an unexpected error happened when trying to resolve "some_previous_project"
Error: Can't resolve 'some_previous_project' in 'C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project'
at getPackageRootDirectory (C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project\node_modules\next-transpile-modules\src\next-transpile-modules.js:70:11)
at Array.map (<anonymous>)
at generateModulesPaths (C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project\node_modules\next-transpile-modules\src\next-transpile-modules.js:81:33)
at withTM (C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project\node_modules\next-transpile-modules\src\next-transpile-modules.js:110:26)
at C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project\node_modules\next-compose-plugins\lib\compose.js:100:23
at Array.forEach (<anonymous>)
at composePlugins (C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project\node_modules\next-compose-plugins\lib\compose.js:77:11)
at C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project\node_modules\next-compose-plugins\lib\index.js:22:38
at normalizeConfig (C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project\node_modules\next\dist\next-server\server\config.js:7:494)
at loadConfig (C:\Users\prajwaldankit\Desktop\truemark\ecom-078\current_project\node_modules\next\dist\next-server\server\config.js:8:131)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! current_project#0.1.0 dev: `next dev`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the current_project#0.1.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\prajwaldankit\AppData\Roaming\npm-cache\_logs\2020-12-11T06_38_48_407Z-debug.log
Your monorepo is wrongly set up as it states in the error message. If you want to use next-transpile-modules with NextJS and monorepo you need to make sure that your monorepo is correctly set up, for which you can use Lerna if you're using npm or yarn workspaces if you're using yarn.
Once that's correctly set up, you need to point your package "main" to be the source of your some_previous_project in order for TM to pick them up. An alternative is to set it up so that your other project creates an ES5 build each time it's changed, but that can quickly occupy your CPU. Another option is to create a Webpack alias in your Next project's config for each of your monorepo modules to resolve imports to src (or wherever your source is), which is what I have done for a project where Next is the development environment and packages have to be able to deploy separately too.
To solve your problem however you first need to make it so that your package resolves, which means you need to set up your monorepo correctly.
I learn Microsoft's manual about TypeScript + React + Redux.
I launched the command:
npm install -S redux react-redux #types/react-redux
But when I run the command npm run start I get the error:
Type error: Could not find a declaration file for module 'react'.
'C:/lab/my-app/node_modules/react/index.js' implicitly has an 'any'
type.
Ok, I run the command: npm i #types/react. now I get other error:
Type error: Module '"../../node_modules/#types/react-redux"' has no
exported member 'Dispatch'. TS2305
How can I fix it?
I had this problem when I initially created a React with TypeScript project, it made no sense since I had not started any work yet.
So in terminal I ran:
rm -rf node_modules/ && npm install
Then:
npm run start
And I was able to see the main landing page.
run the following:
pm install #types/react
It works for me!
I am working on a Firebase server-side code for sending push notifications. To make the code clean, I decided to move some functions to another class which I created and called notificationFunctions.js.
When I do const notificationFunctions = require('notificationFunctions'); at the top of my index.js and call the function from within my sendNotification function, I get an error when deploying my project to the cloud:
⚠ functions[sendNotifications]: Deployment error.
Build failed: exit status 1
npm ERR! Linux 4.4.0-108-generic
npm ERR! argv "/nodejs/bin/node" "/nodejs/bin/npm" "--global-style" "--production" "--fetch-retries=5" "--fetch-retry-factor=2" "--fetch-retry-mintimeout=1000" "install" "/workspace"
npm ERR! node v6.11.5
npm ERR! npm v3.10.10
npm ERR! code E404
npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/notificationFunctions
npm ERR! 404
npm ERR! 404 'notificationFunctions' is not in the npm registry.
npm ERR! 404 Your package name is not valid, because
npm ERR! 404 1. name can no longer contain capital letters
npm ERR! 404 It was specified as a dependency of 'functions'
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! Please include the following file with any support request:
npm ERR! /workspace/npm-debug.log
Functions deploy had errors. To continue deploying other features (such as database), run:
firebase deploy --except functions
I have also added "notificationFunctions": "1.0.0" in my package.json and moved the custom function to node_modules folder.
What is the right way of adding a custom class to Firebase NodeJS?
Edit:
When I follow Doug Stevenson's advise and I remove the custom class from my package.json, and move the class to the same directory as index.js, I still keep getting an error:
i deploying functions
i functions: ensuring necessary APIs are enabled...
✔ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
Error: Error parsing triggers: Cannot find module 'notificationFunctions'
Try running "npm install" in your functions directory before deploying.
Edit 2
Ok. I figured I have to change the way I import my class to require('./notificationFunctions') and this removes all errors when deploying the project.
Once I run the code on the cloud however, I get this error: FIREBASE WARNING: Exception was thrown by user callback. TypeError: notificationFunctions.getNotificationPayload is not a function
The way I call the function from my code is: const payload = notificationFunctions.getNotificationPayload(userLanguage, senderName, groupName, messageContent, messageType);
If you add code to your project in the functions folder, you don't need to make any changes to your package.json dependencies. npm dependencies are only required for modules that are published to the NPM registry.
What your build is doing now is looking for a node module called "notificationFunctions" on NPM, and that obviously doesn't exist there. You should just put your module code along with all the other code under functions and require() it directly from there.
Also, bear in mind that node_modules is not deployed with your code. Cloud Functions will fetch all the npm dependencies on the server side and make them available to your code.