Can’t require global npm module - javascript

I have the following problem. I install uuid with npm install -g uuid.
When I try to run the following code:
var uuid = require("uuid");
console.log(uuid.v1());
an exception is thrown:
module.js:339
throw err;
^
Error: Cannot find module 'uuid'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (d:\CodingProjects\HTML\TestJavascript.js:16:12)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
If I install the module locally with npm install uuid it works fine.
But why is that? What should I do, to make my global packages working?
I tried to download it from Language & Frameworks -> Javascript -> Libraries, but I still get an exception if I don't install it locally for my project.
Thanks in advance.

The reason is in how npm installs packages.
When you run npm install -g <package>, you tell npm to install the <package> globally. This works when you want to use the package as a command line tool, like, for example, Gulp:
$ npm install -g gulp
$ cd path/to/project
$ gulp
However, this does not work when you want to depend on a package.
To depend on a package, you should install it locally, i. e. in the project directory. This is one of the npm’s key benefits: local installation makes managing dependencies and upgrading them much easier for you. The npm install command without -g flag is dedicated exactly for that. When you run
$ npm install uuid
in, say, foo directory, a foo/node_modules directory is created, and uuid module is installed there. After that, you could create a .js file in the foo directory (or any its subdirectory), require the uuid module from it, and everything will work.
As to WebStorm: downloading a library from Language & Frameworks → Javascript → Libraries does not actually download an npm package, it just downloads and installs the library type definitions. Type definitions help WebStorm build better autocompletion when you use a library in your code.

If you would like to use your global packages, all you need to do is create a link between your global package from within your local directory.
This is also known as creating a symbolic link (symlink).
So, running the "npm link uui" in your local directory would allow you to use the global package wihout having to download it into your local directory.
If you analyze the local directory you'll notice that a (linked) folder has been created, meaning it is merely a pointer to the global package.
So, in short, if you want to use global packages, then "npm link" is the way to do it.

Related

Cannot find module '#mdx-js/mdx' - Gatsby

After the last update of today i'm getting this error. I'm using no MDX in my project howsoever..
npm run develop produces this issue, does someone has some knowledge about this?
internal/modules/cjs/loader.js:979
throw err;
^
Error: Cannot find module '#mdx-js/mdx'
Require stack:
- /Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/transform-recipe-mdx.js
- /Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/renderer/index.js
- /Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/validate-steps.js
- /Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/recipe-machine/index.js
- /Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/graphql-server/server.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:976:15)
at Function.Module._load (internal/modules/cjs/loader.js:859:27)
at Module.require (internal/modules/cjs/loader.js:1036:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/transform-recipe-mdx.js:12:35)
at Module._compile (internal/modules/cjs/loader.js:1147:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Module.require (internal/modules/cjs/loader.js:1036:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/transform-recipe-mdx.js',
'/Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/renderer/index.js',
'/Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/validate-steps.js',
'/Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/recipe-machine/index.js',
'/Users/mikey/Sites/Gimmix/node_modules/gatsby-recipes/dist/graphql-server/server.js'
]
}
Problem is solved with the latest update(s): 😁
gatsby#2.24.5
(gatsby-plugin-sharp#2.6.33)
(gatsby-source-contentful#2.3.43)
(gatsby-transformer-remark#2.8.34)
delete your cache
gatsby clean
Be sure to also update your gatsby-cli: npm i -g gatsby-cli
delete your node_modules & packages for a clean install
rm -rf node_modules package-lock.json
last but not least
npm install --save
and test your building proces
npm run develop
I would recommend downgrading to your latest stable version of your Gatsby dependency (to v2.24.37). It seems to be a common issue in ^2.24.54 versions. As you may guess, although you are not using the #mdx-js/mdx package, it's a dependency used by Gatsby itself.
In the upgrade process for any package, when a dependency is upgraded doesn't mean that all their sub-depenencies work because of the needs of each project. It may take a few days/weeks to work for all packages. That's why you should ensure in a gatsby dev and gatsby build that the project works before committing and pushing anything to the repository.
If you haven't committed the upgrade, just rollback the tracked files (git checkout . or git reset --hard), remove your node_modules and run a npm install command to keep the project in your stable running version.
If you have committed your files, run npm install gatsby#2.24.37 to downgrade your Gatsby version (or fix it in your package.json) and run a npm install again.
I saw in your comments that you have the dependency in your package.json but you are not using it. In that case remove it by npm uninstall #mdx-js/mdx.
Running yarn add #mdx-js/mdx fixed it for me.
You first make sure that all dependencies are installed properly.
You can check that is this module installed or not by importing import {<name>} from '#mdx-js'
If it doesn't break it means that it is installed already.
Or if it breaks then run npm install to install all the listed dependencies.
You can move forward and check that this package has that file or data that you want.
You can check this with import {<name>} from '#mdx-js/mdx'. You can also check this with navigating to node_modules/#mdx-js/mdx directory. You will find some names of data you can use, functions or objects etc. Name must match from one of the data type else you have a typo when you are importing something.
Note: this is a general debugging approach for node_modules
run yarn run build and fix those errors. That fixed my problem!

How can I start this Node.JS application?

There is an open-source application which visually displays a difference between two BPMN diagrams.
I want to see what the application looks like when it runs.
How can I start it under Ubuntu?
I tried to run node app.js in the directory bpmn-js-diffing/app but got the error
module.js:341
throw err;
^
Error: Cannot find module 'jquery'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at bpmn-js-diffing/app/app.js:6:11
at Object.<anonymous> (bpmn-js-diffing/app/app.js:435:3)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
I looked at the Gruntfile in search of a "run" command, but only found these
grunt.registerTask('test', [ 'karma:single' ]);
grunt.registerTask('auto-test', [ 'karma:unit' ]);
grunt.registerTask('default', [ 'jshint', 'test', 'browserify:standaloneViewer', 'jsdoc' ]);
To me they look like commands for running automated tests and generating documentation, not for running the actual application.
So how can I start this application?
Update 1: Ran npm install in bpmn-js-diffing directory. Then tried to run node app.js again (in the bpmn-js-diffing/app) directory. Here is the result:
bpmn-js-diffing/app$ node app.js
module.js:341
throw err;
^
Error: Cannot find module 'bpmn-js-diffing'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at bpmn-js-diffing/app/app.js:9:17
at Object.<anonymous> (bpmn-js-diffing/app/app.js:435:3)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
One big part of the Node.Js ecosystem is npm which comes with NodeJS. This is how dependencies are managed.
Many NodeJS programs will have a package.json file which describes various things about them, like for example what are their dependencies. By running npm install NPM will look at what packages the program needs, and install them automatically.
You need to install jquery module. 'npm i jquery'.
Here is a procedure to run this application
I finally found that you didn't even need to execute npm install command to run this project.
Indeed, the app/bpmn-viewer.js file embed all the necessary modules inside its source mapping.
To display the code, either open the Sources tab in Chromium Developer tools or open the 3.2 MB file. The last line begins with // # sourceMappingURL =.
Copy and paste each character from data: application / json; base64,at the end of the line (XX0 =) in the address bar of a browser.
Installation
$ git clone https://github.com/bpmn-io/bpmn-js-diffing
$ cd bpmn-js-diffing
Startup local HTTP server on port 7357
Don't change dir, you must run this command in project root
python -m SimpleHTTPServer 7357
Run
Open http://127.0.0.1:7357/app/ in a browser
Demo (running on Ubuntu 18.04)
You can compare other files by loading them from resources/ directory.
You can use any local HTTP server instead of the embedded python one, for example live-server.
Do do this, run the following commands at project root :
npm install live-server
./node_modules/.bin/live-server
Then browse to http://127.0.0.1:8080/app (default port 8080 can be changed, run live-server --help for more information)
In node js whenever you use require('module_name') it will search the node_module folder in your project directory or it can go to the global node_module folder. If the required module is not there, it will give Cannot find module module_name, you can do npm i module_name to resolve. also, you can save all the required dependencies in package.json folder by using npm i module_name --save command so that if you need to run the same code on the different environment you do not install each module separately you just need to do npm i and if will refer package.json folder and install a the dependencies.
From what I understand, you are trying to run a Node.js module.
I think the app you're trying to run is this
bpmn-js-diffing
is a module designed to be plugged into the above App.
This is a web application not intended to run using NodeJS, but inside a browser. It manipulates the DOM with jQuery; NodeJS does not have a DOM. It uses Grunt and Browserify to build the contents of the /app directory, but it already has the app built. If you want to rebuild it, you'll have to install grunt with npm install -g grunt-cli and executing grunt --force inside the project root, but you don't have to if you don't make any changes and just want to upload the application.
In order to have the web application working, you'll have to upload /app, /assets and /resources to a web root of an online web server and navigate to https://<domain>/app/index.html.
Do this:
1. Replace Diffing = require('bpmn-js-diffing'); with Diffing = require('../index'); in you app.js file.
2. install npm i jsondiffpatch
3. node app.js

How to fix Cannot find module 'typescript' in Angular 4?

I generated Angular 4 app 3 weeks ago using the #angular/cli. After 2 weeks, I tried to run it with the command line ng serve but I am prompted an error below:
Cannot find module 'typescript'
Error: Cannot find module 'typescript'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\mypc\Documents\Angular Projects\my-angular-app\node_modules\#angular\cli\models\config\config.js:5:12)
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)
Do you have any idea how to fix this? Thanks.
For me just running the below command is not enough (though a valid first step):
npm install -g typescript
The following command is what you need (I think deleting node_modules works too, but the below command is quicker)
npm link typescript
This should do the trick,
npm install -g typescript
I was able to solve this problem by removing node_modules then running npm install
If you don't have particular needs, I suggest to install Typescript locally.
NPM Installation Method
npm install --global typescript # Global installation
npm install --save-dev typescript # Local installation
Yarn Installation Method
yarn global add typescript # Global installation
yarn add --dev typescript # Local installation
I had a similar problem when I rearranged the folder structure of a project. I tried all the hints given in this thread but none of them worked.
After checking further I discovered that I forgot to copy an important hidden file over to the new directory. That was
.angular-cli.json
from the root directory of the #angular/cli project. After I copied that file over all was running as expected.
Run: npm link typescript if you installed globally
But if you have not installed typescript try this command: npm install typescript
If you use yarn instead of npm, you can install typescript package for that workspace by running:
yarn add typescript
or you can install it globally by running:
sudo yarn global add typescript
to be available for any project.
I had the same problem. If you have installed first nodejs by apt and then you use the tar.gz from nodejs.org, you have to delete the folder located in /usr/lib/node_modules.
I had a very similar problem after moving a working project to a new subdirectory on my file system. It turned out I had failed to move the file named .angular-cli.json to the subfolder along with everything else. After noticing that and moving the file into the subdirectory, all was back to normal.
Run 'npm install' it will install all necessary pkg .
If you have cloned your project from git or somewhere then first, you should type npm install.
The best way is to do: npm install or npm i
this way all dependencies will be added.
I had this same problem using npm run start-dev as configured my package.json ("start-dev": "nodemon src/index.ts"). When i used nodemon src/index.ts in cmd work just fine, but to use npm run start-dev i had to use npm link typescript, it solve the problem.

Ubuntu trying to run .js file getting throw err;

So i moved to the directory where the .js file is located and i opend it with node filename.js got nodejs and everything installed on the ubuntu VPS and i get error:
throw err;
^
Error: Cannot find module 'is-property'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/www/bot/node_modules/steamcommunity-mobile-confirmations/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/generate-object-property/index.js:1:80)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
what is the problem? or m i trying to run this file completly wrong?
It looks like you need to install a dependency.
Try running the following command in your terminal
npm install is-property --save
If you're trying to run a script with a package.json file, that should define all the dependencies you need. All that's left to do is run npm install in the project directory.
If there's no package.json defining dependencies, you'll need to install each missing dependency. As you've found out, it'll throw a missing module error if they're missing. You might run into this issue multiple times if there are multiple missing modules. To remedy that, either dive into the code, and look for require('package-name') statements that don't reference a local path. (E.g. require('./package') is a local file, whereas require('package') will look for an npm installed module.) Alternatively, you can just keep trying to run it, and install each missing dependency manually until it stops spitting out errors.
I highly recommend creating a package.json if one doesn't exist yet. You can use npm init, and any installed packages should be automatically added to it. Then you can save additional packages to it using npm install package-name --save, or npm install package-name -S. That way, if you use the same code on another machine/in another directory, you don't have to copy over the node_modules folder, as you can specify which packages/versions you want and install them with a single npm install.
Additionally, you may wish to read up on what the package.json file is in relation to node.
NPM Docs on package.json

How do you fork and develop a globally installed Node.js NPM package?

I'm trying to create a fix for a an NPM package that I rather enjoy, Wintersmith. However, Wintersmith is supposed to be globally installed (executable with wintersmith <command>) and I can't seem to run it from the root of a project where I have it installed locally manually with git clone <my fork url> with something like node ./node_modules/wintersmith/bin/wintersmith without receiving path errors:
module.js:340
throw err;
^
Error: Cannot find module '../lib/cli/'
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> (F:\web\wintersmith\node_modules\bin\wintersmith:3:1)
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)
The only way I can figure to properly develop and test this is by manually replacing my globally installed Wintersmith package with my forked version and then executing commands using that version to test whether or not my changes are working.
Is there some workflow step that I'm missing when working with/developing global NPM packages?
You should use npm install git:// instead git clone
Install your fork like this:
npm install git://github.com/YOURNAME/wintersmith.git
You can also install it as global module use -g:
npm install -g git://github.com/YOURNAME/wintersmith.git
NodeJS eats an environment variable NODE_PATH. You can make use of that besides PATH.
This document describes it better:
http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
I can execute wintersmith like this, without having to install it globally:
$ git clone https://github.com/jnordberg/wintersmith.git
$ cd wintersmith
$ npm install # This installs all dependencies I need for the next step.
$ npm run-script prepublish # Run the prepublish script which compiles sources to ./lib
$ bin/wintersmith
I get the usage message that it gives when there is nothing passed as arguments.
When creating a node package that is meant to be installed globally it is possible and desirable to design it so that it can be run and tested without having to install it. I was surprised that wintersmith would not be designed this way.
Generally speaking, if I had to deal with a node package that cannot be tested without being installed globally, I'd call it "defective". (Maybe there are exceptions but such exceptions are rare and they should be justified in the package's documentation.)
So, generally, if I had to deal with a package that cannot be run and tested without being installed globally, I'd look for a package providing equivalent functionality which can be run without being installed globally, or fix the faulty package.

Categories

Resources