简体   繁体   English

NPM package.json依赖项及其在本地和全局中的可执行文件

[英]NPM package.json dependencies and their executables in local vs global

I think npm install|update works when requiring dependencies in source files, but when I want to run an executable, like nodemon , it doesn't appear to work. 我认为npm install|update在需要源文件中的依赖项时有效,但是当我想运行可执行文件时,比如nodemon ,它似乎不起作用。 Is it trying to look for the file globally? 它是否试图在全球范围内查找文件? How can I make these commands look in node_modules first? 如何让这些命令首先在node_modules查找?


I have a Cakefile that starts the dev server with nodemon . 我有一个Cakefile,用nodemon启动dev服务器。 For example: 例如:

# **`procExec(procName)`**
# returns the path to executable in `node_`
procExec = (procName) -> 
    console.log "./node_modules/" + procName + "/bin/" + procName
    "./node_modules/.bin/" + procName

# **`cake startdev`**
# Starts the server with `nodemon`
# Watch and compile `.coffee` and `.styl` files in `/client`

task "startdev", "Starts server with nodemon and watch files for changes", ->
    # start nodemon server
    nodemon = spawn procExec("nodemon"), ["server.coffee"]
    processOutput nodemon

    # watch and compile CoffeeScript
    coffee = spawn procExec("coffee"), ["-o", "public/js/app", "-cw", "client/coffee"]
    processOutput coffee

    # watch and compile Stylus
    stylus = spawn procExec("stylus"), ["client/stylus", "-l", "-w", "-o", "public/css/app"]
    processOutput stylus

It works but with a few minor problems: 它有效,但有一些小问题:

  • npm install|update doesn't seem to install nodemon . npm install|update似乎没有安装nodemon I think it tries to install globally and fails. 我认为它试图全局安装并失败。 I manually did a npm install nodemon separately. 我分别手动执行了npm install nodemon Why is this? 为什么是这样? And how can I tell nodemon to install anyway? 我怎么能告诉nodemon要安装?
  • Does "./node_modules/.bin/" + procName always resolve to the correct executable? "./node_modules/.bin/" + procName是否始终解析为正确的可执行文件?

There's a few questions in here, so I'll try and keep them separated. 这里有几个问题,所以我会尝试将它们分开。

npm install|update doesn't seem to install nodemon. npm install | update似乎没有安装nodemon。 I think it tries to install globally and fails. 我认为它试图全局安装并失败。 I manually did a npm install nodemon separately. 我分别手动执行了npm install nodemon。 Why is this? 为什么是这样? And how can I tell nodemon to install anyway? 我怎么能告诉nodemon还是要安装?

Did you see a warning about "prefer global install"? 您是否看到有关“首选全局安装”的警告? If that's the case it was just a warning and it would've been installed anyways. 如果是这种情况,它只是一个警告,它将会被安装。 If it was a different error please include the output. 如果是不同的错误,请包括输出。

Does "./node_modules/.bin/" + procName always resolve to the correct executable? “./node_modules/.bin/”+ procName是否始终解析为正确的可执行文件?

Yes, any scripts listed in the package.json files of your dependencies will be installed to this folder. 是的,依赖项的package.json文件中列出的任何脚本都将安装到此文件夹中。 However, I much prefer using the npm bin command to always get the correct path. 但是,我更喜欢使用npm bin命令来始终获取正确的路径。

If you are spawning processes from node, you can also require('npm') and modify process.env.PATH to get the correct node_modules/.bin in place. 如果要从节点生成进程,还可以require('npm')并修改process.env.PATH以获取正确的node_modules/.bin Eg at the top of your Cakefile: 例如,在Cakefile的顶部:

npm = require 'npm'
npm.load (err) -> throw err # If config fails to load, bail out early
process.env.PATH = npm.bin + ":" + process.env.PATH
# Now you no longer need to use procExec in your tasks

disclaimer I don't know if modifying PATH like that will work on windows. 免责声明我不知道修改PATH是否适用于Windows。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM