skip to Main Content

I’m currently using Eclipse 2020-06 with Node.js 14 and created a "General" project with a single .js file to test some Javascript code. I opened a command prompt and ran npm install [email protected] to install jsdom but this code:

const jsdom = require("jsdom");

… caused the following error when I ran the .js file:

Error: Cannot find module ‘jsdom’

I cd-ed to the project folder and ran the install command again, now the code runs without any errors.

How do I install this specific version of jsdom in a way that all current and future Eclipse projects are going to find it too, without having to install it (= run the command) for every single project? There’s no option to access the build path for this project ("no actions available") and so far I haven’t been able to find any information about how to link to it with an environment variable or if there’s a global setting for it in Eclipse.

3

Answers


  1. You can install any package for global access:

    npm install jsdom -g

    In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.

    See the link below for more information:

    https://docs.npmjs.com/cli/v10/commands/npm-install

    NOTE: you still have to add jsdom entry to your package.json dependencies property in the distribution version of your package. Still, you can import the jsdom package from any script running through the NodeJS executable after the jsdom package is installed globally.

    Login or Signup to reply.
  2. check your path when you download jsdom.
    if the name of project is blabla , the path will be …>…>blabla
    or
    try this`

    npm i jsdom-global

    instead of this

    npm install [email protected]

    `

    Login or Signup to reply.
  3. Don’t install your project modules globally or scoped to Eclipse, even if you can do it with -g. Modules should be installed locally and saved into the project’s package.json.

    Reasons:

    1. If you check your project into version control, which is the common case scenario for any moderately serious work, then when you pull down the project on another machine or try to get it working again at some point in the future, you’ll have no idea which versions of all the packages will be necessary to run it.
    2. If you want to run two projects at the same time that have different version requirements for a specific package, they’ll fight each other and you may see confusing compatibility errors frequently, compounding as you add more projects.

    So avoid npm/dependency hell and do the normal thing of using a specific package.json for each project. Run npm i within each project folder to give it a node_modules folder tailored to each project’s specific needs.

    The one exception to this are certain common CLI tools that are intended to be installed globally, like @angular/cli. JSDOM doesn’t fall into this category and should be installed on a per-project basis, without the -g flag.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search