npm start script with babel-node and dotenv

Viewed 4424

I have this script which works well

"start": "nodemon -x node -r dotenv/config src/index.js"

I want use babel-node instead node. So there is the new script:

"start": "nodemon -x babel-node --presets=env -r dotenv/config -- src/index.js"

But I get this error

[nodemon] starting `babel-node --presets=env -r dotenv/config src/index.js`
internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'pathTo/dotenv/config'

Can you help ?

6 Answers

This worked for me

    "start": "nodemon --exec babel-node -r node_modules/dotenv/config index.js"

This issues existed for a short time in babel. @babel/node cannot seem to resolve node modules, when using --require flag

  "start": "nodemon --exec babel-node -r ./node_modules/dotenv/config src/index.js"

This issue has now been resolved. Allow -r from node_modules with @babel/node

  "start": "nodemon --exec babel-node -r dotenv/config src/index.js"

I think that you should check pathTo/dotenv/confit resource path. I know Babel node bundle file has very strict path rule.

yes,

"start": "nodemon -x babel-node -r node_modules/dotenv/config --presets=env src/index.js"

seems to work

EDIT In fact this definitively does not works

I know this is a reasonably old thread, but here is what worked for me. Full disclosure, I don't really know why this works, but it did. Files condensed for relevancy.

Environment

Windows 10
Node 10.13.0
NPM 6.4.1
Babel 7.2.3
Nodemon 1.18.9
Dotenv 6.2.0

package.json:

{
    "scripts": {
        "dev": "nodemon src/index.js -- --require node_modules/dotenv/config"
    }
}

nodemon.json:

{
    "execMap": {
        "js": "babel-node --presets @babel/preset-env"
    }
}

I use the npm package "env-cmd" for this situation.

  "scripts": {
    "start": "env-cmd --file ./path/to/.env nodemon --exec babel-node index.js"
  }
Related