Giter Site home page Giter Site logo

navigator is not defined about cuid HOT 12 CLOSED

paralleldrive avatar paralleldrive commented on August 29, 2024
navigator is not defined

from cuid.

Comments (12)

ericelliott avatar ericelliott commented on August 29, 2024

What version are you using? Are you sure it's cuid causing the error?

The node version of cuid shouldn't give you any trouble with browser stuff.

from cuid.

 avatar commented on August 29, 2024

"cuid": "^1.2.4" and node v0.12.7... I'll double check again where the error is coming from.

from cuid.

ericelliott avatar ericelliott commented on August 29, 2024

The latest version of cuid is 1.2.5. Try that, first.

from cuid.

 avatar commented on August 29, 2024

1.2.5 Didn't resolve the problem, either. Maybe something else is conflating what's really going on. I'm trying to run tests via mochajs - and that's where the navigator is not defined error's are coming from. This is expected, since node processes don't have a window.navigator - but I thought cuid supported node and browsers... which is what makes me think something is tricking cuid into believing it's actually inside a browser.

Interestingly I was able to reacreate the problem outside of my app. I created a my-test.js file that looks like this...

describe('work', function () {
    var cuid = require('cuid')
    it('should work', function() {
        var canevaluate = typeof navigator == "undefined";
        var id = cuid()
        console.log('ID', id)
        console.log('canEval', canevaluate)
        expect(id).to.equal(id)
    })
})
  1. run browserify -t babelify -e ./spec-support/index.js my-test.js -o compiled.js
  2. run mocha compiled.js
  work
    1) should work


  0 passing (9ms)
  1 failing

  1) work should work:
     ReferenceError: navigator is not defined
      at Function.browserPrint (specs.js:12825:17)
      at cuid (specs.js:12784:27)
      at Context.<anonymous> (specs.js:8:18)

Does this mean the browserify pre-compile step is evaluated as "hey we're in the browser, navigator is here... no worries" and then chokes when the code is evaluated again inside node? On another note - the latest version is 1.2.5, yet github doesn't have that release tagged? I'm seeing a strange difference in package.json files too.

github/cuid/package.json

  "version": "1.2.5",
  "main": "./build/server/cuid.js",
  "browserify": "./build/client/cuid.js",

node_modules/cuid/package.json

  "version": "1.2.5",
  "main": "./dist/node-cuid.js",
  "browserify": "./dist/browser-cuid.js",

from cuid.

ericelliott avatar ericelliott commented on August 29, 2024

Interesting. It looks to me like the server version isn't even getting built. @therealklanni

from cuid.

ericelliott avatar ericelliott commented on August 29, 2024

I strongly suspect this is related to issue 42. Does the current version work for you?

from cuid.

 avatar commented on August 29, 2024

It doesn't, but the issue may very well still be on my end. I'm thinking the problem is that when running browserify to compact all the **/tests/*-spec.js the environment is node set to --node, which means your package.json "browser": "/dist/browser-cuid.js" is getting pulled in instead. Bummer.

from cuid.

therealklanni avatar therealklanni commented on August 29, 2024

@ericelliott looking into it. This was working before, might be related to the recent changes, trying to nail it down.

In short, the "navigator is not defined" is caused by the fact that node doesn't have a concept of a navigator, so for tests to run they need to be run in a browser, not in node.

from cuid.

ericelliott avatar ericelliott commented on August 29, 2024

@therealklanni Yeah, but in a Node build, the "main" module should be used, not the "browser" version. The browser version should only be pulled in for browserify / webpack builds targeting browsers. AFAIK, this is configured correctly in our package.json, so I'm a little puzzled about why this is happening.

Here's an ugly workaround in the meantime: specify the exact path/filename to import the correct package instead of import cuid from 'cuid';.

from cuid.

therealklanni avatar therealklanni commented on August 29, 2024

I think this is related to #42. I believe once we finish resolving that issue it will shed some light on this.

from cuid.

jdoleary avatar jdoleary commented on August 29, 2024

I just ran into this issue trying to use the new node debugger built into Chrome Dev Tools

from cuid.

ericelliott avatar ericelliott commented on August 29, 2024

If you want to try to get resolve the build/CI test failures in this PR it should solve this issue.

from cuid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.