Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use $npm_package variables #115

Closed
g-div opened this issue Oct 30, 2015 · 5 comments
Closed

Use $npm_package variables #115

g-div opened this issue Oct 30, 2015 · 5 comments

Comments

@g-div
Copy link

g-div commented Oct 30, 2015

nar is a really great project and I would like to use it to deploy my apps, but I'm experiencing a problem with my package.json.

I use to write my package.json like this:

{
  "name": "testpackage",
  "version": "0.0.1",
  "description": "A test package",
  "main": "index.js",
  "archive": {
    "dependencies": true,
    "devDependencies": false,
    "globalDependencies": [
      "npm"
    ]
  },
  "scripts": {
    "start": "node $npm_package_main",
    "build": "nar create --executable"
  },
  "devDependencies": {
    "nar": "^0.3.20"
  }
}

npm start works, but if I create an executable with nar create --executable and run it, I will get the following error:

Extracting files...
Extract [global-dependency] npm
Extract [package] testpackage
Running testpackage-0.0.1.nar
Run [start]: /usr/bin/env bash ~/testpackage/.nar/nar/scripts/node.sh $npm_package_main
Running application...
> module.js:338
>     throw err;
>     ^
> 
> Error: Cannot find module '/tmp/nar-testpackage-0.0.1-1446201800070/$npm_package_main'
>     at Function.Module._resolveFilename (module.js:336:15)
>     at Function.Module._load (module.js:286:25)
>     at Function.Module.runMain (module.js:475:10)
>     at startup (node.js:118:18)
>     at node.js:952:3
Error: Command failed with exit code: 1

The start script isn't able to recognize package.json variables.
Is there a way to achieve this ?

@h2non
Copy link
Owner

h2non commented Oct 30, 2015

Unfortunately npm built-in variables are not supported, but I would like to support it soon.

In the meantime, you can explicitly pass the version as environment variable, but you have to use a sightly different notation:

 "scripts": {
    "start": "node ${VERSION}"
 }

Or alternatively, you can run the script via npm (as you're embedding it):

 "scripts": {
    "start": "npm run boot",
    "boot": "node $npm_package_main"
 }

@g-div
Copy link
Author

g-div commented Oct 30, 2015

Thanks. I've wrapped my start script as in your second example and it works perfectly.
It would still be useful to have access to npm variables.

@h2non
Copy link
Owner

h2non commented Oct 30, 2015

Great! I've created another issue as feature request for npm variables. See #116

@h2non h2non closed this as completed Oct 30, 2015
g-div added a commit to g-div/nodeplate that referenced this issue Oct 30, 2015
@mend3
Copy link

mend3 commented Sep 27, 2018

Unfortunately npm built-in variables are not supported, but I would like to support it soon.

In the meantime, you can explicitly pass the version as environment variable, but you have to use a sightly different notation:

 "scripts": {
    "start": "node ${VERSION}"
 }

Or alternatively, you can run the script via npm (as you're embedding it):

 "scripts": {
    "start": "npm run boot",
    "boot": "node $npm_package_main"
 }

I know it's a bit late, but for further Windows devs:

If you're on Windows, you must wrap your npm_package_whatever with %

In the given case, the script

"boot": "node $npm_package_main"

would become:

"boot": "node %npm_package_main%"

@dandv
Copy link

dandv commented Jan 7, 2021

Note that $npm_package_* variables are no longer automatically placed in the environment since NPM v7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants